我正在写一个嵌入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

创建带有依赖的jar时,Maven不会引发任何错误。

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

另外,尽管我需要解决此问题,但我也感谢任何建议的解决方法,因为我需要尽快生成服务器的可执行演示,而无需Eclipse即可执行Pointy-Haired Boss(tm)。

解决方案:

根据Pavel的回答,我放弃了maven-assemly-plugin,转而使用maven-shade-plugin。这是对我有用的阴影配置:
        <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 http://stackoverflow.com/a/6743609/589215-->
                            <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>

最佳答案

您没有正确合并 Jersey jar 。

Jersey 1.x使用META-INF/services机制来发现其组件和装配体:单个可能只是将所有内容复制到单个jar中,覆盖已存在的文件,但需要将META-INF/services文件进行连接。

尝试使用jersey-bundle(com.sun.jersey:jersey-bundle:1.14)或修复您的程序集设置(或找到另一个插件来做得更好)。

07-24 09:47
查看更多