I'm creating a webservice method that can serve xml, json and csv output:
@GetMapping(produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE, "text/csv"})
public Rsp get() {
}
Problem: I somehow need to add the text/csv
content type to my configureContentNegotiation()
. But how? Because a String is not accepted, and a MediaType.TEXT_CSV
does not exist (even though RFC7111 defines it as a valid 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
}
}
.mediaType("csv", new MediaType("text", "csv"));
or even configure as follows in application.properties
:
spring.mvc.media-types.json=application/json
spring.mvc.media-types.xml=application/xml
spring.mvc.media-types.csv=text/csv
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments