27

I'm writing a server that embeds Jetty w/ Jersey. When I execute from Eclipse, everything is great. However, if I assemble my server and all dependencies into a single jar using Maven's assembly:single goal, I get an exception:

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]

...

The full trace is here, if it's useful: https://gist.github.com/3790817

Maven throws no errors while creating the jar-with-dependencies.

I'm a novice with Maven and deployment of Java, and I'm really not sure how to proceed with debugging.

Also, while I need to solve this issue I'd also appreciate any suggested work-arounds as I need to produce an executable demo of my server ASAP that a Pointy-Haired Boss (tm) can execute without Eclipse.

Solution:

Based on Pavel's answer, I dropped the maven-assemly-plugin in favor of maven-shade-plugin. Here's the shade configuration that worked for me:

        <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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
HolySamosa
  • 9,011
  • 14
  • 69
  • 102
  • Is it possible to post the (jersey related) dependencies defined in your pom file? I have the same issue, but I am already using the maven-shade-plugin. Switching to `jersey-bundle` solved my problem, but would like to avoid it an define only the needed dependencies in my pom. – Athafoud Apr 26 '15 at 17:21
  • This solution worked for me. I had exactly the same issue. Thanks!!!! – jmsimpson68 Jul 13 '15 at 18:58

2 Answers2

24

You are not merging Jersey jars correctly.

Jersey 1.x uses META-INF/services mechanism to discover its components and assembly:single probably just copies everything into single jar, overriding already present files BUT META-INF/services file(s) needs to be CONCATENATED.

Try using jersey-bundle (com.sun.jersey:jersey-bundle:1.14) or fix your assembly settings (or find another plugin to do it better).

Athafoud
  • 2,898
  • 3
  • 40
  • 58
Pavel Bucek
  • 5,304
  • 27
  • 44
  • 1
    Thanks, Pavel. You nailed it. In my case, the `jersey-bundle` presented problems of it's own however I had success dropping the `maven-assembly-plugin` plugin and moving to the `maven-shade-plugin`. I'll be updating my question w/ POX excerpt that did the trick. – HolySamosa Sep 27 '12 at 16:46
  • Could you post your pom ? – supercobra Sep 07 '14 at 13:55
  • Simply adding the jersey-bundle dependency and retaining the use of the maven-assembly-plugin fixed my jar-with-dependencies. For what it's worth. – Mafro34 Nov 17 '14 at 01:03
1

Could you post your pom ?

Do you mark some dependencies as provided ? It's something quite different to build a standalone app and a webapp, as some jars a supposed to be provided by the web container (tomcat or other).

As your container is "embedded" in your app (and not your app in the container) then maybe you don't manage correctly these dependencies.

Samuel EUSTACHI
  • 3,116
  • 19
  • 24