spring restful webservice中的java返回JsonObject
我使用的是spring框架。我在Wepsphere服务器上有一个这样的Web服务
@RequestMapping (value="/services/SayHello2Me" , method=RequestMethod.GET, headers="Accept=application/json")
@ResponseBody
public JSONObject SayHello2Me(HttpServletRequest request) throws Exception {
String input = (String) request.getParameter("name");
String output = "hello " + input + " :)";
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj;
}
当我以Chrome的形式调用它,比如http://myserver/services/sayHello2Me?name='baris'时,它会返回一个错误:
Error 404: SRVE0295E: Error reported: 404
如果我在我的Web服务中这样更改注释
@RequestMapping (value="/services/SayHello2Me")
@ResponseBody
public JSONObject SayHello2Me(HttpServletRequest request) throws Exception {
String input = (String) request.getParameter("name");
String output = "hello " + input + " :)";
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj;
}
然后,当我以类似http://myserver/services/sayHello2Me?name='baris'的形式调用它时,它会返回错误:
Error 406: SRVE0295E: Error reported: 406
有一个jsonobject问题,因为如果我在同一个webservice中返回字符串而不是jsonobject,它会成功返回我
如何在spring restful webservice中返回Jsonobject
# 1 楼答案
你应该使用
return outputJsonObj.toString();
如下所示# 2 楼答案
你可以使用jackson: