【问题标题】:Jersey exception only thrown when depencencies assembled into a single jar仅当依赖项组装到一个 jar 中时才会抛出 Jersey 异常
【发布时间】:2012-09-18 15:19:54
【问题描述】:

我正在编写一个嵌入 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-with-dependencies 时不会抛出任何错误。

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

另外,虽然我需要解决这个问题,但我也很感谢任何建议的解决方法,因为我需要尽快生成我的服务器的可执行演示,尖头 Boss (tm) 可以在没有 Eclipse 的情况下执行。

解决方案:

根据 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>

【问题讨论】:

  • 是否可以发布您的 pom 文件中定义的(与球衣相关的)依赖项?我有同样的问题,但我已经在使用 maven-shade-plugin。切换到jersey-bundle 解决了我的问题,但想避免它在我的 pom 中仅定义所需的依赖项。
  • 这个解决方案对我有用。我有完全相同的问题。谢谢!!!!

标签: java maven jar jersey executable-jar


【解决方案1】:

您没有正确合并 Jersey jars。

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

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

【讨论】:

  • 谢谢,帕维尔。你搞定了。在我的情况下,jersey-bundle 提出了它自己的问题,但是我成功删除了maven-assembly-plugin 插件并移至maven-shade-plugin。我将使用 POX 摘录更新我的问题。
  • 你能发布你的 pom 吗?
  • 只需添加 jersey-bundle 依赖项并保留使用 maven-assembly-plugin 即可修复我的 jar-with-dependencies。物有所值。
【解决方案2】:

你能发布你的 pom 吗?

您是否将某些依赖项标记为已提供? 构建一个独立的应用程序和一个 webapp 是完全不同的,因为一些 jars 应该由 web 容器(tomcat 或其他)提供。

由于您的容器“嵌入”在您的应用中(而不是容器中的应用),那么您可能没有正确管理这些依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2018-09-11
    • 2013-03-29
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    相关资源
    最近更新 更多