I am trying to get my Spring Boot application to return accept=text/csv
, I continue to get:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
I've added:
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:${jackson_version}"
To my build.gradle
, I am NOT using SpringMVC
and the handler looks like:
@RequestMapping(value = "/{id}/csv", method = RequestMethod.GET, produces = "text/csv")
public List<RegistrationCode> exportToCsv(@ApiParam(name = "id", required = true, value = "string") @PathVariable String id, HttpServletResponse response) {
...
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
csvFileName);
response.setHeader(headerKey, headerValue);
response.setContentType("text/csv;charset=utf-8");
return registrationCodes;
}
Curl example:
curl -X GET --header 'Accept: text/csv' --header 'Authorization: Bearer ...' 'http://localhost:8080/api/1001/csv'
Message Converter:
public class CsvMessageConverter extends AbstractHttpMessageConverter<List<RegistrationCode>> {
}
Example adding message converts (in traditional applicationContext.xml
):
<util:list id="messageConverters">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
p:objectMapper-ref="jsonObjectMapperFactory"/>
<!--bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"
p:objectMapper-ref="xmlObjectMapperFactory"/-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="com.binding.CsvMessageConverter"/>
</util:list>