【问题标题】:How to add csv MimeType to spring content-negotiation?如何将 csv MimeType 添加到 spring 内容协商?
【发布时间】:2017-06-27 11:09:25
【问题描述】:

我正在创建一个可以提供 xml、json 和 csv 输出的 webservice 方法:

@GetMapping(produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE, "text/csv"})
public Rsp get() {
}

问题:不知何故,我需要将text/csv 内容类型添加到我的configureContentNegotiation()。但是怎么做?因为不接受字符串,并且不存在 MediaType.TEXT_CSV(即使 RFC7111 将其定义为有效的 mimetype。

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
                .favorParameter(true)
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .mediaType("csv", "text/csv"); //this is invalid
    }
}

【问题讨论】:

  • 在 Spring Boot 应用程序中,您可以将 spring.mvc.media-types.csv = text/csv 放在 application.properties 中

标签: spring csv spring-mvc


【解决方案1】:
.mediaType("csv", new MediaType("text", "csv"));

甚至在application.properties中配置如下:

spring.mvc.media-types.json=application/json
spring.mvc.media-types.xml=application/xml
spring.mvc.media-types.csv=text/csv

【讨论】:

  • 很好的例子!它对我有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2014-06-15
  • 2016-01-05
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多