3
votes

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

2
What is Image like?cassiomolin
With MOXy, wrap your List<Image> into a class and return it. Or change the JSON provider to Jackson.cassiomolin
Make sure Image is annotated with @XmlRootElement. This is required with MOXy, as it uses JAXB under the hoodPaul Samsotha
Ok i found the solution: jersey-media-json-jackson has to be the first dependency :) .. wtf .. EDITED in the questionSupercop89
same problem here. moving dependency on top worked. BUT WHY???besil

2 Answers

5
votes

I found the answer in comment by Supercop89:

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

0
votes

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.