Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return JsonObject in spring restful webservice

I am using spring framework. I have a webservice in Wepsphere server like that

@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 :

Error 404: SRVE0295E: Error reported: 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 :

Error 406: SRVE0295E: Error reported: 406

There is a jsonobject problem because if I return String insted of jsonobject in same webservice it returns me successfully.

How can I return Jsonobject in spring restful webservice?

like image 755
neverwinter Avatar asked Apr 17 '15 11:04

neverwinter


People also ask

How do I return a JSON object to a REST web service?

Create RESTEasy Web Service to Produce JSON with @BadgerFish Now create a class whose methods will be exposed to the world as web service. Use JBoss @BadgerFish annotation that supports to return response as JSON. To return JSON as response we need to use media type as application/json.

How consume JSON from RESTful web service in spring boot?

Create a simple Spring Boot web application and write a controller class files which is used to redirects into the HTML file to consumes the RESTful web services. We need to add the Spring Boot starter Thymeleaf and Web dependency in our build configuration file. For Maven users, add the below dependencies in your pom.


2 Answers

406-Not Acceptable Response

you should use return outputJsonObj.toString(); try below as

@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();
} 
like image 74
Pravin Avatar answered Oct 12 '22 12:10

Pravin


You can use jackson:

@RequestMapping (value="/services/SayHello2Me" , method=RequestMethod.GET, produces="application/json")
like image 33
Nimesh Avatar answered Oct 12 '22 12:10

Nimesh