1

I'm trying to use Jersey RS to return JSON for a POJO. This is not a servlet and there is no servlet container. The Jersey is being setup from within Netty (not Jetty). I can get Jersey to return APPLICATION_XML but APPLICATION_JSON on same resource throws an exception saying No Message Body writer for my domain class was found. I have included jersey-json and jackson-jaxrs modules in pom.xml.

Here's how the ResourceConfig is being setup :

Map<String, Object> props = new HashMap<String, Object>();
props.put(PackagesResourceConfig.PROPERTY_PACKAGES, RESOURCES_PACKAGE);
props.put(PROPERTY_BASE_URI, "http://localhost:9000/");
props.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);    
ContainerFactory.createContainer(JerseyHandler.class, new PackagesResourceConfig(props));

where JerseyHandler is my Netty Handler class. The resource is very simple:

@Path("/test")
public class TestResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public TestData get() {
           return new TestData();
    }

TestData is the simplest model class:

@XmlRootElement
public class TestData {
   private String firstName;
   private String lastName;

   public TestData() {}
   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   } 

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
}

Here's the exception when trying to access "/test" :

SEVERE: A message body writer for Java class com.xyz.models.TestData, and Java type class com.xyz.models.TestData, and MIME media type application/json was not found

I have tried including Context classes from here but still no luck.

I can't imagine returning JSON from Jersey can be that hard. All examples on internet assume you're using Jersey from a servlet container but I am not! thanks for any inputs.

Community
  • 1
  • 1
Praveen Ray
  • 83
  • 1
  • 8

1 Answers1

0

Do you have jackson-core and jackson-mapper librairies ? That is what we need to get JSON serialization working.

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • mvn dependency:tree shows both jackson-core and jackson-mapper-asl modules are pulled in by jersey-json. Any other ideas? – Praveen Ray Mar 28 '12 at 20:23