SpringBoot如何以JSON格式返回errorResponse?

SpringBoot如何以JSON格式返回errorResponse?,json,rest,spring-boot,httpresponse,Json,Rest,Spring Boot,Httpresponse,我是实施SpringBoot+RestfulWebservice的新手。在我的项目中,使用@ControllerAdvice全局处理异常,并创建自定义类以字符串格式设置错误代码和错误消息。我们正在将错误对象传递给HTTPResponse并重试响应。但我想知道错误消息如何转换为JSON格式,因为我们没有显式使用任何httpMessageConverter - Is Spring Boot internally do the conversion? 请帮助我理解这种行为。尽管您觉得这是一个基本问题

我是实施SpringBoot+RestfulWebservice的新手。在我的项目中,使用@ControllerAdvice全局处理异常,并创建自定义类以字符串格式设置错误代码和错误消息。我们正在将错误对象传递给HTTPResponse并重试响应。但我想知道错误消息如何转换为JSON格式,因为我们没有显式使用任何httpMessageConverter

- Is Spring Boot internally do the conversion?

请帮助我理解这种行为。尽管您觉得这是一个基本问题,但请给我一些见解。

基本上,当您没有提供有关内容类型协商的详细信息时,Spring Boot在所有方面都默认使用JSON

根据spring引导文档,使用
@ControllerAdvice
处理错误时默认生成JSON格式的错误。从文档中:

您还可以定义一个用@ControllerAdvice注释的类,以自定义JSON文档,以返回特定控制器和/或异常类型,如以下示例所示:

在mkyong.com上有一篇关于如何定制ControllerAdvice错误处理的文章

作为JSON用法的一个示例,这个非常简单的控制器将尽最大努力将任何POJO呈现为JSON(注意,如果您只提供像
long
这样的原语,它将以文本形式返回原语,但将内容类型设置为
application/JSON


默认情况下,springboot在启动时注册以下
HttpMessageConverters

ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
ByteArrayHttpMessageConverter–转换字节数组
StringHttpMessageConverter–转换字符串
ResourceHttpMessageConverter–为任何类型的八位字节流转换org.springframework.core.io.Resource
SourceHttpMessageConverter–转换javax.xml.transform.Source
FormHttpMessageConverter–将表单数据与多值映射进行转换。
Jaxb2RootElementHttpMessageConverter–将Java对象转换为XML或从XML转换为Java对象(仅当类路径上存在JAXB2时添加)
MappingJackson2HttpMessageConverter–转换JSON(仅当类路径上存在Jackson 2时添加)
MappingJacksonHttpMessageConverter–转换JSON(仅当类路径上存在Jackson时添加)
AtomFeedHttpMessageConverter–转换Atom提要(仅在类路径上存在Rome时添加)
RssChannelHttpMessageConverter–转换RSS提要(仅当类路径上存在Rome时添加)
因此,如果您正在从控制器建议返回一个对象,spring将在默认情况下自动调用Jackson转换器将该对象转换为有效的Json响应


基本上,springboot会检查
MIME
类型,以决定使用
HttpMessageConverter
的哪个实现。此外,它还将取决于“Accept”头来决定调用方期望的数据类型。如果收到的是请求,springboot将使用“Content type”页眉决定要发送的数据类型。

谢谢你,Anantha的澄清。@satyanehru很乐意帮忙。@satyanehru很乐意帮忙。
$ curl -v http://localhost:8080/api/time
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /api/time HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 04 Feb 2020 04:18:35 GMT
<
* Connection #0 to host localhost left intact
{"time_in_nanos":47078744054692}
$ curl -v http://localhost:8080/api/time --header 'Accept: application/xml'
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /api/time HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: application/xml
>
< HTTP/1.1 406
< Content-Length: 0
< Date: Tue, 04 Feb 2020 04:17:38 GMT
<
* Connection #0 to host localhost left intact
* Closing connection 0
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)