在 Spring Boot 中返回 JSON 对象作为响应 [英] Returning JSON object as response in Spring Boot

查看:73
本文介绍了在 Spring Boot 中返回 JSON 对象作为响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Boot 中有一个示例 RestController:

@RestController@RequestMapping("/api")类 MyRestController{@GetMapping(path = "/hello")公共 JSONObject sayHello(){return new JSONObject("{'aa':'bb'}");}}

我正在使用 JSON 库 org.json

当我点击 API /hello 时,我收到一条异常消息:

<块引用>

Servlet.service() for servlet [dispatcherServlet] 在上下文中与路径[] 抛出异常 [请求处理失败;嵌套异常是java.lang.IllegalArgumentException: 找不到要返回的转换器类型值:类 org.json.JSONObject] 具有根本原因

java.lang.IllegalArgumentException: 找不到要返回的转换器类型值:class org.json.JSONObject

这是什么问题?有人能解释一下到底发生了什么吗?

解决方案

当您使用 Spring Boot Web 时,Jackson 依赖项是隐式的,我们不必明确定义.如果使用 Eclipse,您可以在依赖层次结构选项卡中的 pom.xml 中检查 Jackson 依赖.

并且由于您已使用 @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

您面临的问题是因为返回的对象(JSONObject)没有某些属性的 getter.您的意图不是序列化这个 JSONObject,而是序列化一个 POJO.所以只需返回POJO.
参考此链接: https://stackoverflow.com/a/35822500/5039001>

如果你想返回一个 json 序列化的字符串,那么只需返回字符串.在这种情况下,Spring 将使用 StringHttpMessageConverter 而不是 JSON 转换器.

I have a sample RestController in Spring Boot:

@RestController
@RequestMapping("/api")
class MyRestController
{
    @GetMapping(path = "/hello")
    public JSONObject sayHello()
    {
        return new JSONObject("{'aa':'bb'}");
    }
}

I am using the JSON library org.json

When I hit API /hello, I get an exception saying :

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: No converter found for return value of type: class org.json.JSONObject

What is the issue? Can someone explain what exactly is happening?

解决方案

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.

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

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆