在Spring Boot中返回JSON对象作为响应 [英] Returning JSON object as response in Spring Boot
问题描述
我在Spring启动时有一个示例Rest Controller:
I have a sample Rest Controller in Spring boot:
@RestController
@RequestMapping("/api")
class MyRestController
{
@GetMapping(path = "/hello")
public JSONObject sayHello()
{
return new JSONObject("{'aa':'bb'}");
}
}
我正在使用json库:org.json
I am using the json library : org.json
当我点击api / hello时,我得到一个异常说:
When I hit the api /hello, I get an exception saying :
servlet [dispatcherServlet]的Servlet.service()在路径为
[]的上下文中引发异常[请求处理失败;嵌套异常是
java.lang.IllegalArgumentException:找不到返回
值的转换器类型:class org.json.JSONObject]具有根本原因
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject] with root cause
java.lang.IllegalArgumentException:找不到返回
类型的转换器类:class org.json.JSONObject
java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject
什么是问题。有人可以解释究竟发生了什么。我是SpringBoot的新手。
What is the issue. Can someone explain what exactly is happening. I am new to SpringBoot.
在此先感谢:)
推荐答案
当您使用spring boot web时,jackson依赖是隐式的,我们不必明确定义。如果使用eclipse,您可以在依赖关系层次结构选项卡中的pom.xml中检查jackson依赖项。
As you are using spring boot web, jackson dependency is implicit and we do not have to define explicitly. You can check for jackson dependency in your pom.xml in the dependency hierarchy tab if using eclipse.
并且因为您已使用注释@RestController
不需要进行显式的json转换。只需返回一个POJO,jackson序列化器将负责转换为json。它与@Controller一起使用时相当于使用 @ResponseBody
。不是在每个控制器方法上放置 @ResponseBody
,而是放置 @RestController
而不是vanilla @Controller
和 @ResponseBody
默认情况下应用于该控制器中的所有资源。
请参阅此链接: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody
And as you have annotated with @RestController
there is no need to do explicit json conversion. Just return a POJO and jackson serializer will take care of converting to json. It is equivalent to using @ResponseBody
when used with @Controller. Rather than placing @ResponseBody
on every controller method we place @RestController
instead of vanilla @Controller
and @ResponseBody
by default is applied on all resources in that controller.
Refer this link: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody
您面临的问题是因为返回的对象(JSONObject)没有某些属性的getter。并且您的目的不是序列化此JSONObject,而是序列化POJO。所以只需返回POJO。
请参阅此链接: https://stackoverflow.com/ a / 35822500/5039001
The problem you are facing is because the returned object(JSONObject) does not have getter for certain properties. And your intention is not to serialize this JSONObject but instead to serialize a POJO. So just return the POJO.
Refer this link: https://stackoverflow.com/a/35822500/5039001
如果要返回json序列化字符串,则只返回字符串。在这种情况下,Spring将使用StringHttpMessageConverter而不是JSON转换器。
If you want to return a json serialized string then just return the string. Spring will use StringHttpMessageConverter instead of JSON converter in this case.
这篇关于在Spring Boot中返回JSON对象作为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!