vlio20
vlio20

Reputation: 9295

Jersey return a JSONObject

is it possible to return a JSONObject in Jersey? Here is what I am trying to achieve:

@Path("/path")
@get
@Produces(MediaType.APPLICATION_JSON)
public JSONObject foo()
{
    ...
    JSONObject json = getJsonObject();
    return json;
} 

I tried to code the above but got some errors regarding the getters and setters of the JSONObject class.

Any advice?

Here is the stacktrace I am getting:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
    org.codehaus.jackson.map.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:52)
    org.codehaus.jackson.map.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:25)
    org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
    org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:558)
    com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
    com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

Upvotes: 1

Views: 5951

Answers (2)

Alessandroempire
Alessandroempire

Reputation: 1699

In my case for some reason

 @Produces("application/json");

was not returning a json.

So instead I used:

 @produces(MediaType.APPLICATION_JSON);

An alternative.

Upvotes: 1

Gianluigi Pierini
Gianluigi Pierini

Reputation: 98

First, you can return a json with just putting

@Produces("application/json")

With that Jersey will know that the output is in json format, but I don’t know why you use JSONObject, you can use any Class and the server will transform it in json with just putting

@XmlRootElement
public class MyClass {} 

MyClass now can be used like a return type, so is more simple and convenient in that way.

Example:

@XmlRootElement
public class MyClass{
}

@GET
@Consumes("application/json")
public MyClass putJson(){
return new MyClass();
}

The client of the services will see a json that represent all the atributes of the Class.

PD:If you have a exception like MessageBodyProviderNotFoundException you need to add Genson in your dependencies.

Upvotes: 1

Related Questions