Search code examples
jax-rsjersey-2.0glassfish-4

MessageBodyWriter not found for media type=application/json even while Jackson is included


I've seen some solutions to this problem but it seems like following them doesn't solve my problem. I am returning a simple string array and when I return it, I am getting this error:

Severe:   MessageBodyWriter not found for media type=application/json, type=class [Ljava.lang.String;, genericType=class [Ljava.lang.String;.

The method is really simple (gets a list of files)

   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public String[] getSegments(@PathParam("userId") String userId,
           @PathParam("deviceId") String deviceId) {
       System.out.println("in get, userId passed: " + userId);
       String[] segments = storage.getSegments(userId, deviceId);  
       return segments;
   }

My POM.XML seems to have all the things recommended in other posts:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>RoverServerLib</artifactId>
        <version>${project.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.7.0</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-servlet-portability</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.connectors</groupId>
        <artifactId>jersey-apache-connector</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
       <groupId>org.glassfish.jersey.media</groupId>
       <artifactId>jersey-media-moxy</artifactId>
       <version>${jersey.version}</version>
   </dependency>
</dependencies>

Solution

  • The problem is with MOXy. It doesn't know how to handle String arrays. When you have MOXy on the classpath, it is the used as the default. To use Jackson (jersey-media-json-jackson). You just need to register the JacksonFeature. This will disable MOXy. Note that even just removing MOXy from your project, will have no affect, as Glassfish has it, so you need to make sure to register the JacksonFeature.

    You should also get rid of jackson-jaxrs-json-provider. jersey-media-json-jackson already pulls it in (but a different version).

    Another thing is that Glassfish already has it's own version of Jersey and friends. If you don't mark all your Jersey related dependencies as <scope>provided</scope>, then it's possible you may have version conflicts. So you should make them all provided. Glassfish 4.1 uses Jersey 2.10.4. If you need some newer version features, I would recommend updating Jersey in Glashfish.