如何在 spring-web servlet 中接受 text/csv 作为 content-type?

How to accept text/csv as content-type in spring-web servlet?

我想创建一个生成 text/csv 内容的简单网络服务。但我不能要求它:

@RestController
public class MyServlet {
    @PostMapping(produces = {"text/csv", "application/json"})
    public Object post() {
        //...
    }
}

spring.mvc.contentnegotiation.media-types.csv=text/csv

当我使用 Content-Type: text/csv 作为 http header 发送 post 请求时,出现以下错误:

415: Content type 'text/csv' not supported

这是我的配置:

@Configuration
public class ContentNegotiationConfiguration implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
                .favorParameter(true) //favor &format=csv
                .defaultContentType(MediaType.APPLICATION_JSON)
                .parameterName(format);
                //.mediaType("csv", new MediaType("text", "csv")) //also tried without success
    }
}

如果您(客户端)需要 csv 内容,客户端应将 text/csv 作为 Accept header 而不是 Content-Type.