具有下载文件端点的基于HAL的REST API

时间:2017-10-17 10:27:52

标签: java spring rest spring-boot spring-hateoas

我正在使用Spring启动开发基于HAL的REST API。我需要一个控制器中的端点,它将一个文件发送到客户端。有一些关于SO的例子,但由于以下例外情况它不起作用:

Resolved exception caused by Handler execution: 
  org.springframework.http.converter.HttpMessageNotWritableException: Could not write 
  content: No serializer found for class java.io.ByteArrayInputStream and no properties 
  discovered to create BeanSerializer (to avoid exception, disable 
  SerializationFeature.FAIL_ON_EMPTY_BEANS) through reference chain: 
  org.springframework.core.io.ByteArrayResource["inputStream"]); 
  nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
  No serializer found for class java.io.ByteArrayInputStream 
  and no properties discovered to create BeanSerializer (to avoid exception, 
  disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: 
  org.springframework.core.io.ByteArrayResource["inputStream"])

My Spring应用程序:

@EnableHypermediaSupport(type = HAL)
@SpringBootApplication
public class MyServer {
    public static String CURIE_NAMESPACE = "myNS";

    public @Bean
    CurieProvider curieProvider() {
        return new DefaultCurieProvider(CURIE_NAMESPACE, new UriTemplate("/docs/html5/{rel}.html"));
    }

    @Bean
    public HttpMessageConverters customConverters() {
        ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        return new HttpMessageConverters(arrayHttpMessageConverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyServer.class, args);
    }
}

我的Controller类看起来像这样:

@BasePathAwareController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyController implements ResourceProcessor<RepositoryLinksResource> {
    public static final String ENDPOINT_URL = "/myResource";
    ...

    @RequestMapping(value = ENDPOINT_URL + "/download", method = RequestMethod.GET)
    public ResponseEntity<ByteArrayResource> downloadAttachment(){
        ...
        // get from my service the resource
        MyClass myResource = myService.getResource();

        String filename = myResource.getFilename();
        String contentType = myResource.getContentType();

        try (InputStream myStream = myResource.getStream()) {
            HttpHeaders respHeaders = new HttpHeaders();
            respHeaders.setContentType(MediaType.valueOf(contentType));
            respHeaders.setContentDispositionFormData("attachment", filename);

            byte[] bytes = IOUtils.toByteArray(myStream);
            ByteArrayResource byteResource = new ByteArrayResource(bytes);

            return ResponseEntity.ok()
                   .headers(respHeaders)
                   .contentType(MediaType.parseMediaType(contentType))
                   .body(byteResource);
        } catch (Exception e) {
        ...
        }
    }
}

我正在添加自定义转换器bean,如我的应用程序代码所示。但我认为由于这个问题@EnableHypermediaSupport is not compatible with Spring Boot's Jackson2ObjectMapperBuilder #333它不起作用!?

spring-boot版本:1.5.2.RELEASE

spring-hateoas版本:0.23.0.RELEASE

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的邮件转换器与内容类型不匹配。使用ResourceHttpMessageConverter或将您的返回类型更改为ResponseEntity&lt; byte []&gt;。