Question

I have a JAX-RS WebService with the following method:

@Path("/myrest")
public class MyRestResource {
...
    @GET
    @Path("/getInteger")
    @Produces(APPLICATION_JSON)
    public Integer getInteger() {
        return 42;
    }

When accessed using this snipped:

@Test
public void testGetPrimitiveWrapers() throws IOException {
    // this works:
    assertEquals(new Integer(42), new ObjectMapper().readValue("42", Integer.class));
    // that fails:
    assertEquals(new Integer(42), resource().path("/myrest/getInteger").get(Integer.class));
}

I get the following exception:

com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class java.lang.Integer, and Java type class java.lang.Integer, and MIME media type application/json was not found
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are: application/json
...

The problem is just with returning single primitive values (int/boolean) or their wrapper classes. Returning other POJO classes is not the problemen so I guess all the answers regarding JSONConfiguration.FEATURE_POJO_MAPPING and JAXB annotations do not apply here. Or which annotation should I use to describe the return type if I don't have access to its class source?

Using ngrep I can verify that just the String "42" is returned by the webservice. Thats a valid JSON "value" but not a valid JSON "text" according to the spec. So is my problem on the client or the server side?

I tried activating JSONConfiguration natural/badgerfish according to http://tugdualgrall.blogspot.de/2011/09/jax-rs-jersey-and-single-element-arrays.html but with no success (ngrep still shows just "42"). Would that be the right path?

Any ideas are appreciated!

Was it helpful?

Solution

This is a recognized bug in Jackson, which has been touted (incorrectly in my opinion) as a feature. Why do I consider it a bug? Because while serialization works, deserialization definitely does not.

In any case, valid JSON cannot be generated from your current return type, so I would recommend creating a wrapper class:

class Result<T> {
    private T data;

    // constructors, getters, setters
}

@GET
@Path("/getInteger")
@Produces(APPLICATION_JSON)
public Result<Integer> getInteger() {
    return new Result<Integer)(42);
}

Alternatively, you can elect to wrap root values, which will automatically encapsulate your data in a top level JSON object, keyed by the objects simple type name - but note that if this option is used that all generated JSON will be wrapped (not just for primitives):

final ObjectMapper mapper = new ObjectMapper()
    .configure(SerializationFeature.WRAP_ROOT_VALUE, true)
    .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

final String serializedJson = mapper.writeValueAsString(42);
final Integer deserializedVal = mapper.readValue(serializedJson,
        Integer.class);

System.out.println(serializedJson);
System.out.println("Deserialized Value: " + deserializedVal);

Output:

{"Integer":42}
Deserialized Value: 42

See this answer for details on how to retrieve and configure your ObjectMapper instance in a JAX-RS environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top