在spring restful webservice中返回JsonObject

nev*_*ter 6 java rest spring web-services 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;
      }
Run Code Online (Sandbox Code Playgroud)

当我将其称为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;
  } 
Run Code Online (Sandbox Code Playgroud)

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

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

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

如何在spring restful webservice中返回Jsonobject?

Pra*_*vin 6

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();
} 
Run Code Online (Sandbox Code Playgroud)