【发布时间】:2015-06-24 05:53:12
【问题描述】:
我正在使用弹簧框架。我在 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;
}
当我像 http://myserver/services/sayHello2Me?name='baris' 这样从 Chrome 中调用它时,它会返回那个错误:
错误 404:SRVE0295E:报告错误:404
如果我像这样更改我的网络服务中的注释
@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' 这样从 Chrome 中调用它时,它会返回那个错误:
错误 406:SRVE0295E:报告错误:406
存在 jsonobject 问题,因为如果我在同一个 Web 服务中返回 String insted of jsonobject,它会成功返回。
如何在spring restful webservice中返回Jsonobject?
【问题讨论】:
-
映射不匹配。当您将其更改为在两个地方都相同时可以吗?
-
所有映射都匹配。只是我在这里写错了。我更新了我的问题。
-
你为什么不直接返回一个 POJO? Jackson 处理其余部分并生成 JSON 字符串。
-
恕我直言,在这种情况下,您的请求不会发送到myserver/services/sayHello2Me,而是myserver/services/sayHello2Me{name}
标签: java spring web-services rest jsonobject