0

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

  • Have you tried ["application/json"](http://incubator.apache.org/wink/1.0/html/JAX-RS%20Request%20and%20Response%20Entities.html)? – Thomas Mar 02 '12 at 18:33
  • Yes, tried that, same result. –  Mar 02 '12 at 18:41
  • Odd error. The definition looks correct, except that if that is an org.json.JSONObject then I'm not sure its serializable. Try changing your return type to String, to ensure that all the pathing at least is correct. Also if you can, post your call signature. – Perception Mar 02 '12 at 18:47
  • Changing return to type String gives me the same error. Not sure what 'call signature' is, the class is annotated with '@Path(value="/customers")' and I hot the service with the URL: 'http://localhost:9080/testWeb/ws/customers' - I have a plain text GET working fine in the same class, but can't get JSON going. :( –  Mar 02 '12 at 19:09
  • @RobertHume - the call signature is typically the http verb, request url, and headers. It should look something like `GET http://localhost:9080/testWeb/ws/customers HTTP/1.1 ... `. – Perception Mar 02 '12 at 20:06
  • @Perception - Thanks for the help. I've decided to move forward with a webservice that accepts params via a standard http POST for now. I need to get it working, I'll revisit the JSON approach later and post a solution if I find out what's wrong. Thanks again. –  Mar 02 '12 at 20:13

1 Answers1

0

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);
}
Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77