javaspringspring-restspring-web

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


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
    }
}

Solution

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