java 在 Spring Restful webservice 中返回 JsonObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29698193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return JsonObject in spring restful webservice
提问by neverwinter
I am using spring framework. I have a webservice in Wepsphere server like that
我正在使用弹簧框架。我在 Wepsphere 服务器中有一个这样的网络服务
@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;
}
When I call it form Chrome like http://myserver/services/sayHello2Me?name='baris', it returns me that error :
当我像http://myserver/services/sayHello2Me?name='baris'这样从 Chrome 调用它时,它返回给我那个错误:
Error 404: SRVE0295E: Error reported: 404
错误 404:SRVE0295E:报告的错误:404
If I change annotions in my webservice like that
如果我像这样更改网络服务中的注释
@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;
}
then when I call it form Chrome like http://myserver/services/sayHello2Me?name='baris', it returns me that error :
然后当我像http://myserver/services/sayHello2Me?name='baris'这样从 Chrome 调用它时,它返回给我那个错误:
Error 406: SRVE0295E: Error reported: 406
错误 406:SRVE0295E:报告的错误:406
There is a jsonobject problem because if I return String insted of jsonobject in same webservice it returns me successfully.
有一个 jsonobject 问题,因为如果我在同一个 webservice 中返回 jsonobject 的字符串,它会成功返回我。
How can I return Jsonobject in spring restful webservice?
如何在 Spring Restful webservice 中返回 Jsonobject?
采纳答案by Pravin
406-Not Acceptable Response
406-不可接受的响应
you should use return outputJsonObj.toString();
try below as
你应该使用return outputJsonObj.toString();
下面的尝试作为
@RequestMapping (value="/services/SayHello2Me")
@ResponseBody
public String 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.toString();
}
回答by Nimesh
You can use Hymanson:
您可以使用Hyman逊:
@RequestMapping (value="/services/SayHello2Me" , method=RequestMethod.GET, produces="application/json")