Robert Hume Robert Hume - 3 months ago 11
Java Question

what's wrong with my simple JSON JAX-RS web service?

I have a simple web service written with Apache Wink 1.0, I want to receive and return JSON data.

According the Wink docs, this should work ...

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject postJSON(JSONObject requestJSON) {
JSONObject jobj = new JSONObject();
return jobj;
}


... but I see this error when I try to hit the web service ...

org.apache.wink.server.internal.handlers.PopulateResponseMediaTypeHandler -
Content-Type not specified via Response object or via @Produces annotation
so automatically setting via generic-type compatible MessageBodyWriter providers


... any advice or suggestions are greatly appreciated!

Rob

Answer

Usage of JSONObject is a little bit strange. Easier and more flexible approach:

public MyDto postJSON(MyDto dto) {
  //do something
  MyDto md = new MyDto();
  return md;
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyDto {
  private String f1;
  private int f2;
  //etc.
}

JAX-RS would serialize MyDto do JSON. In fact, even cleaner approach is to return Response object

public Response postJSON(MyDto dto) {
    //do something
    MyDto md = new MyDto();
    return Response.ok(md);
}
Comments