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

这一生的挚爱 提交于 2019-12-12 03:11:46

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/9538342/whats-wrong-with-my-simple-json-jax-rs-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!