Spring Boot

Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error

投稿日:

Spring Boot 2.0.0.BUILD-SNAPSHOT にて、RestController のレスポンスでファイルをダウンロードさせようとしたところ、

@RequestMapping("api/v1/m4v")
public class M4vRestController {

    @Autowired
    SystemConfiguration systemConfiguration;

    @RequestMapping(value = "/", method = RequestMethod.POST, produces = "video/mp4")
    Resource downloadFile(@RequestBody @Validated M4vFile m4vFile){
        String filePath = m4vFile.getName();
        return new FileSystemResource(filePath);
    }

このエラー。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.nio.channels.Channels$1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.nio.channels.Channels$1 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.core.io.FileSystemResource["outputStream"])
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:986) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
...
Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.nio.channels.Channels$1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.nio.channels.Channels$1 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.core.io.FileSystemResource["outputStream"])
...
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.nio.channels.Channels$1 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.core.io.FileSystemResource["outputStream"])

WebMvcConfigurer を実装したクラスで configureMessageConverters を Override していたのが原因ぽい。このクラス自体を削除するとエラーも無くなる。

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }
}

果たしてこれが最適な解なのかどうか分からないが・・・。デフォルト値を false に。もっとスマートな方法があるはず・・・。そもそも RestController ではファイルではなくてファイルのダウンロード URL を返すべきとかそういう話も。でもファイルを static な場所に置くのもあれだし・・・、

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // これ追加
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }
}

-Spring Boot

Copyright© hirooka.pro , 2013-2018 All Rights Reserved.