Spring RESTful Webservice - Returning JSON without model object

心不动则不痛 提交于 2020-02-24 20:19:27

问题


there.
I have a doubt with returning JSONObjects on a Spring RESTful WebService.

Here it goes:
I have a method in my controller which I want to have it returning a JSONObject. However, when I set it's return type to JSONObject and effectively return a JSONObject, I get that following error:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject

So, I actually understand what that means, and I've been searching a answer to that issue for at least 3 days.

Here is my code:

@RequestMapping(value = "/value", method = RequestMethod.POST)
public String method(HttpServletRequest request) {
    JSONObject json = new JSONObject();
    json.put("example", "example message");

    return json.toString();
}

I trully don't know if that's gonna work when I have to consume it on the front-end (Which is going to be an external application). Do I have to return a true JSONObject? Or returning a JSONObject.toString() should work fine?

And one last thing:
Most tutorials about returning a JSONObject teaches that proccess using a model object, which I don't want to use. Is there a way of doing that without using a model object?

Thanks in advance, peeps!


回答1:


I had the same issue, and the solution is unbelievably simple. I assume that you have Jackson in your dependencies, and then you can do as follows:

After you create a JSONObject you want to return, simply write: return jsonObject.toMap(), and Jackson will do the rest of the work. Don't forget to change the return type of your method to Map<String, Object>, and apply appropriate annotations: @ResponseBody on the method, or @RestController on a whole class. It depends on your needs.

In your case it will be:

@RequestMapping(value = "/value", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> method(HttpServletRequest request) {
    JSONObject json = new JSONObject();
    json.put("example", "example message");

    return json.toMap();
}



回答2:


Just annotate your method with @ResponseBody.




回答3:


Change class annotation to @RestController, then return model object or Map<String, Object>. You don't need to mess with JSONObject for the task.

@ResponseBody for the method, like akhill answered, is also ok but too verbose if all the controller is REST.




回答4:


If you are using Jackson for mapping you can return an ObjectNode instead. It is Jackson's representation of a json object. There is also an ArrayNode class. If you want to use your own implementation then either write an HttpMessageConverter that allows reading and writing json or use the toString method if this actually returns a json string.




回答5:


You can also use anonymous object:

new Object() {
    public final int code = 0;

    public final List<T> data = ...;
}


来源:https://stackoverflow.com/questions/37380913/spring-restful-webservice-returning-json-without-model-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!