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?
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.
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.
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();
}
You can use jackson:
@RequestMapping (value="/services/SayHello2Me" , method=RequestMethod.GET, produces="application/json")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With