0

I'm writing a simple Jersey REST web service where I'm consuming JSON object and sending another JSON object. This is just for the workaround. It throws the following error.

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) )

Ajax code

        $('#click').click(function() {
        $.ajax({
            url: "http://localhost:8080/test/rest/myService/jsonpost",
            method: "POST",
            data: jsonObj,
            dataType: 'application/json',
            contentType: "application/json",
             success: function(result){
                  alert(result);
             },
             error(){
                 alert("error");
                 console.log('Error');
             }
        });

    });

My code is :

@POST
@Path("/jsonpost")
@Consumes(MediaType.APPLICATION_JSON)
//@Produces(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON })
public Response consumeJSON( Track track ) {

    System.out.println("In post example");
    String output = track.toString();
    System.out.println("Post data"+output);
    JSONObject obj = new JSONObject();
    obj.put("1", "Shane");
    obj.put("2", "Bond");
    System.out.println(obj);

    return Response.status(200).entity(obj.toString()).build();
    //return Response.status(200).entity("success").build();
}

Whole error log

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) )
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.failForEmpty(StdSerializerProvider.java:90)
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:63)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:587)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:245)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1145)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:520)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Syed
  • 2,471
  • 10
  • 49
  • 89

2 Answers2

1

If you use org.codehaus.jackson.map.ObjectMapper, then pls use the following lines in your code.

mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
Gokul
  • 931
  • 7
  • 16
0

Jackson is meant to be used with POJOs (like your Track class). It doesn't know anything about or how to serialize those org.json objects. I would recommend to just use POJOs. The application will be a lot more maintainable with domain classes that clearly defining the domain.

With that being said, it is still possible to make it work with the org.json object or arbitrary key/value collections.

  1. You can simple return a String instead of the JSONObject

    return Response.ok(obj.toString());
    
  2. You can use a Map<String, String> instead. Jackson knows how to serialize those.

  3. There is a support module for org.json, but it's only available for Jackson 2.x (com.fasterxml not org.codehaus).

    If you want to use Jackson 2.x, you can check out this post. Then you need to register the JsonOrgModule seen in the earlier link. You can do that in a ContextResolver, as seen in this post. Going this route, here is your checklist

    • Make sure you have the Jackson 2 dependency.
    • Make sure you register the Jackson packages with Jersey
    • Remove the older Jackson dependency
    • Implement the ContextResolver where you register the JsonOrgModule. This will allow Jackson to know about the org.json objects.

If you need help configuring the last option, please update your post with your web.xml and your dependencies

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks . I've used as return Response.status(200).entity(obj.toString()).build();. But still I'm going into error call back of Ajax. – Syed Jul 28 '16 at 11:19