当依赖项组成一个单独的jar时,Jersey异常才会被抛出。

27

我正在编写一个嵌入Jetty w/ Jersey的服务器。在Eclipse上执行时一切正常。但是,如果我使用Maven的assembly:single目标将服务器和所有依赖项汇集到一个单独的jar文件中,我会得到一个异常:

Sep 26, 2012 5:35:59 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.acme.server.webservice.
exception.WebServiceFailure, and Java type class com.acme.server.webserv
ice.exception.WebServiceFailure, and MIME media type application/json was not fo
und
Sep 26, 2012 5:35:59 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type
are:
*/* ->
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter

17:35:59.372 [qtp184245201-22 - /] ERROR o.a.h.ReflectorServletProcessor - onReq
uest()
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A mess
age body writer for Java class com.acme.server.webservice.exception.WebS
erviceFailure, and Java type class com.acme.server.webservice.exception.
WebServiceFailure, and MIME media type application/json was not found
        at com.sun.jersey.spi.container.ContainerResponse.write(ContainerRespons
e.java:285) ~[vma-server-0.0.1-SNAPSHOT-jar-with-dependencies.jar:na]
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequ
est(WebApplicationImpl.java:1457) ~[server-0.0.1-SNAPSHOT-jar-with-dependenc
ies.jar:na]

如果有用的话,完整的跟踪信息在这里:https://gist.github.com/3790817

Maven在创建包含依赖项的jar时没有报错。

我对Maven和Java部署是新手,不确定如何进行调试。

此外,在解决此问题的同时,我也需要任何建议的解决方法,因为我需要尽快生成可执行的服务器演示,供“尖头老板”(tm)在没有Eclipse的情况下执行。

解决方法:

根据Pavel的答案,我放弃了maven-assemly-plugin,转而使用maven-shade-plugin。 这是对我有用的shade配置:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                        <!--  use transformer to handle merge of META-INF/services - see http://java.net/jira/browse/JERSEY-440?focusedCommentId=14822&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_14822 -->
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        </transformers> 
                        <filters>
                            <!--  filter to address "Invalid signature file" issue - see https://dev59.com/qHNA5IYBdhLWcg3wWsUA#6743609-->
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

能否发布在pom文件中定义的(Jersey相关)依赖项?我也遇到了同样的问题,但我已经在使用Maven Shade插件。切换到 "jersey-bundle" 解决了我的问题,但我想避免这种情况并只在我的pom文件中定义所需的依赖项。 - Athafoud
这个解决方案对我很有效。我遇到了完全相同的问题。谢谢!!! - jmsimpson68
2个回答

24

您没有正确地合并Jersey jars。

Jersey 1.x使用META-INF/services机制来发现其组件和汇编。assembly:single可能只会将所有内容复制到单个jar中,覆盖已经存在的文件BUT META-INF/services文件需要被连接。

尝试使用jersey-bundle (com.sun.jersey:jersey-bundle:1.14)或修复您的汇编设置(或找到另一个插件以更好地完成此操作)。


1
谢谢,Pavel。你做得很好。在我的情况下,jersey-bundle 本身就存在问题,但是我成功地删除了 maven-assembly-plugin 插件并转向使用 maven-shade-plugin。我将更新我的问题,并附上解决问题的 POX 摘录。 - HolySamosa
你能发一下你的pom文件吗? - supercobra
只需要添加jersey-bundle依赖并保留使用maven-assembly-plugin即可修复我的jar-with-dependencies问题。值得一提的是。 - Mafro34

1

你能发布你的pom文件吗?

你是否将某些依赖项标记为提供的?构建独立应用程序和Web应用程序是非常不同的,因为一些JAR包应该由Web容器(如Tomcat或其他容器)提供。

由于你的容器是“嵌入”在你的应用程序中(而不是你的应用程序嵌入在容器中),所以也许你没有正确地管理这些依赖项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接