2

考虑以下:

@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
    return new Account("1", "sample");
}

Accept请求中没有指定标头,但默认情况下仍将响应转换为JSON使用 Spring Boot 时的响应。该@ResponseBody注释在其文档中未提及任何有关存在转换的信息

4

4 回答 4

1
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>
于 2019-01-16T12:21:23.667 回答
0

Spring-boot 应用程序在 POM.xml 的依赖项中使用spring-boot-starter-web。该特定依赖项会下载更快的xmls jackson-datatype,该数据类型在我们使用@springbootapplication 时初始化。

于 2019-01-16T11:19:00.303 回答
0

Spring 默认使用 Jackson/Json(通过在类路径中找到它),但您可以配置自己的:

@Configuration
public class MixInWebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customHttpMessageConverter());
    }
}

请参阅HttpMessageConverter API

于 2019-01-16T11:16:05.370 回答
0

在 @requestMapping 中,您可以添加变量,例如 Produces 或 Consumes 例如:

consumes = MediaType.APPLICATION_JSON_VALUE 
produces = MediaType.APPLICATION_JSON_VALUE
于 2019-01-16T11:27:22.523 回答