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

membersound :

I want to create a simple webservice that produces text/csv content. But I cannot request it:

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

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

When I send a post request with Content-Type: text/csv as http header, I'm getting the following error:

415: Content type 'text/csv' not supported

This is my configuration:

@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
    }
}
Reith :

If you (client) expect csv content, client should pass text/csv as Accept header not Content-Type.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100050&siteId=1