jerseyjersey-2.0spring-jersey

Configuring POJOMappingFeature without web.xml


I am using Jersey to marshall a Java object to JSON, as follows:

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("/test")
public Response replay(String input) throws IOException {
    return Response.ok().entity(new MyClass()).build();
}

Am receiving the following exception:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.company.MyClass, and Java type class com.company.MyClass, and MIME media type application/octet-stream was not found.

I understand that the solution here is to :

  1. Add the jackson-jaxrs-json-provider dependency
  2. Use com.sun.jersey.api.json.POJOMappingFeature

POJOMappingFeature is normally configured in web.xml. Is there an alternative for applications which are annotation driven and do not use web.xml?

Thanks


Solution

  • You should just be able to add this to your pom file.

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
    

    This dependency is normally commented out if you have created your project from the Jersey maven archetype.

    com.sun.jersey.api.json.POJOMappingFeature is only for version 1.* of Jersey. The package name changed from com.sun.* to org.glassfish.* from version 1 to 2 of Jersey.