繁体   English   中英

在spring restful webservice中返回JsonObject

[英]Return JsonObject in spring restful webservice

我正在使用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'时,它会返回该错误:

错误404:SRVE0295E:报告错误:404

如果我在我的webservice中更改注释

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

然后,当我把它称为Chrome,如http:// myserver / services / sayHello2Me?name = 'baris'时,它会返回我的错误:

错误406:SRVE0295E:报告错误:406

有一个jsonobject问题,因为如果我在同一个webservice中返回jsonobject的String insted,它会成功返回给我。

如何在spring restful webservice中返回Jsonobject?

406-不可接受的回应

你应该使用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();
} 

你可以使用杰克逊:

@RequestMapping (value="/services/SayHello2Me" , method=RequestMethod.GET, produces="application/json")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM