Jersey - Maven - MessageBodyWriter not found for media type=application/json
Asked Answered
D

2

5

Currently i have the problem that everything in Netbeans my webservice works but if i start the jar file with the command "java -jar FILENAME PARAMETERS there is the following error.

MessageBodyWriter not found for media type=application/json, type =class java.util.ArrayList, genericType=java.util.List

I need an expert do solve this problem :/. It is very strange because when i execute the jar in Netbeans it works.

pom.xml

http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>at.schneider.development</groupId>
<artifactId>PhotoBoothImageService</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>PhotoBoothImageService</name>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>[0.4, 0.5)</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                            <mainClass>at.schneider.development.photoboothimageservice.Main</mainClass>
                      </manifest>
                    </archive>
            </configuration>
            <executions>
              <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<properties>
    <jersey.version>2.26-b02</jersey.version>
    <jdk.version>1.8</jdk.version>
</properties>

Function:

@GET
@Path("/getimages")
@Produces(MediaType.APPLICATION_JSON)
public List<Image> getImages() {
    availableImages = getImageListFromDirectory(Paths.get(configuration.getProperties().get(BASE_DIR).toString()));
    return availableImages;
}

Thanks in advice!

Best Regards

Derris answered 14/6, 2017 at 19:3 Comment(10)
What is Image like?Diamagnetic
With MOXy, wrap your List<Image> into a class and return it. Or change the JSON provider to Jackson.Diamagnetic
Make sure Image is annotated with @XmlRootElement. This is required with MOXy, as it uses JAXB under the hoodSolis
@CássioMazzochiMolin i attached my image ClassDerris
Ok i found the solution: jersey-media-json-jackson has to be the first dependency :) .. wtf .. EDITED in the questionDerris
same problem here. moving dependency on top worked. BUT WHY???Grot
Indeed this is the solution, why not reply yourself and note that this solved it?Accompanist
@Grot Maybe the project has dependency on the same jar but with different version and somehow the dependencies got mixed and the system was unable to locate the correct class.Accompanist
idem, it works at the top. I compared all transitive dependency versions... all the same!Levi
ok, I added an answer about the why's and a link to a way to fix itLevi
A
9

I found the answer in comment by Supercop89:

jersey-media-json-jackson has to be the first dependency.

Ankeny answered 12/9, 2018 at 8:52 Comment(2)
This just saved my sanity!Sussman
And this is also the case with jersey-media-moxyTholos
L
2

Because maven-assembly-plugin squashes META-INF/services files, it seems the JacksonAutoDiscoverable SPI is gone.

By putting the dependency jersey-media-json-jackson at the top, it ends-up in a better state.

Here is a diff:

JSON FEATURES NOT WORKING

META-INF/services/org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable

org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable

JSON FEATURES WORKING

META-INF/services/org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable:

org.glassfish.jersey.jackson.internal.JacksonAutoDiscoverable

So, putting the dependency at the top will make Json features work, at the expense of some other stuff that you (maybe) don't need.

There are some ways to merge the services in maven-assembly-plugin: Merging META-INF/services files with Maven Assembly plugin

See also: https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html

EDIT: I played a lot with maven-assembly-plugin as described in the links, but wasn't able to make that work. Eventually, I switched to maven-shade-plugin and it worked on my first try. It also displays information on what it does:

[INFO] Including org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.28 in the shaded jar.

If maven-assembly-plugin had done so, I wouldn't have to search for the problem for 1 hour. Also, it didn't need a bunch of xml. Personal conclusion: much easier with the latter.

Levi answered 15/5, 2019 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.