2017-01-24 7 views
1

Я пытаюсь получить мою Spring приложения загрузки, чтобы вернуть accept=text/csv, я по-прежнему получаю:Spring загрузки Производит текст/CSV, не удалось найти приемлемые Представительские

org.springframework.web.HttpMediaTypeNotAcceptableException: Не удалось найти приемлемого представление

Я добавил:

compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}" 
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:${jackson_version}" 

В моих build.gradle, я НЕ используя SpringMVC

и обработчик выглядит следующим образом:

@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 пример:

curl -X GET --header 'Accept: text/csv' --header 'Authorization: Bearer ...' 'http://localhost:8080/api/1001/csv' 

конвертер сообщение:

public class CsvMessageConverter extends AbstractHttpMessageConverter<List<RegistrationCode>> { 

} 

Пример добавления новообращенных сообщений (в традиционном 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> 
+0

Мы протестировали почтальон –

+0

@ GaëlMarziou сделал я мое сообщение конвертируется (я просто не могу 't получить его для регистрации) Я опубликую заголовок этого. –

+0

Я не знал, что вы можете использовать @RequestMapping без использования Spring MVC. В чем польза? Вы не можете использовать springfox также для чванства, правильно? –

ответ

1
//Adding the message converter 
@Configuration 
public class MyApplicationConfiguration { 
    ...  
    @Bean 
    public HttpMessageConverters customConverters() { 
     return new HttpMessageConverters(new CsvMessageConverter()); 
    } 
} 

Я был в состоянии найти некоторые инструкции для регистрации обращенного здесь:

In Spring Boot, adding a custom converter by extending MappingJackson2HttpMessageConverter seems to overwrite the existing converter