如何在 spring-web servlet 中接受 text/csv 作为内容类型?

mem*_*und 6 java spring spring-web spring-rest

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

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

spring.mvc.contentnegotiation.media-types.csv=text/csv
Run Code Online (Sandbox Code Playgroud)

当我发送带有 http 标头的 post 请求时Content-Type: text/csv,出现以下错误:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ith 4

如果您(客户端)期望 csv 内容,则客户端应text/csv作为Acceptheader 传递,而不是Content-Type