考虑以下:
@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
return new Account("1", "sample");
}
Accept
请求中没有指定标头,但默认情况下仍将响应转换为JSON
使用 Spring Boot 时的响应。该@ResponseBody
注释在其文档中未提及任何有关存在转换的信息
考虑以下:
@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
return new Account("1", "sample");
}
Accept
请求中没有指定标头,但默认情况下仍将响应转换为JSON
使用 Spring Boot 时的响应。该@ResponseBody
注释在其文档中未提及任何有关存在转换的信息
By default, A controller return JSON on spring boot project. But If you want XML format then you can configure this on the pom.xml. For example, you can add this following dependency if you want to return XML data,
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Spring-boot 应用程序在 POM.xml 的依赖项中使用spring-boot-starter-web。该特定依赖项会下载更快的xmls jackson-datatype,该数据类型在我们使用@springbootapplication 时初始化。
Spring 默认使用 Jackson/Json(通过在类路径中找到它),但您可以配置自己的:
@Configuration
public class MixInWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customHttpMessageConverter());
}
}
在 @requestMapping 中,您可以添加变量,例如 Produces 或 Consumes 例如:
consumes = MediaType.APPLICATION_JSON_VALUE
produces = MediaType.APPLICATION_JSON_VALUE