2017-10-17 8 views
0

스프링 부트로 HAL 기반 REST API를 개발 중입니다. 컨트롤러에 파일을 클라이언트로 전송하는 엔드 포인트가 필요합니다. 이 SO에 대한 몇 가지 예입니다,하지만 다음 이유로 인해 예외 작동하지 않습니다다운로드 파일 엔드 포인트가있는 HAL 기반 REST API

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"]) 

내 봄 응용 프로그램 :

내 컨트롤러 클래스는 다음과 같습니다
@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); 
    } 
} 

:

@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) { 
     ... 
     } 
    } 
} 

내 응용 프로그램 코드에 표시된대로 사용자 지정 변환기 빈을 추가하고 있습니다. 하지만이 isssue 인해 작동하지 않습니다 생각 @EnableHypermediaSupport is not compatible with Spring Boot's Jackson2ObjectMapperBuilder #333 생각하십니까?

봄 부팅 버전 : 1.5.2.RELEASE

스프링 hateoas 버전 : 0.23.0.RELEASE

어떤 아이디어?

답변

0

메시지 변환기와 콘텐츠 형식이 일치하지 않습니다. ResourceHttpMessageConverter를 사용하거나 리턴 유형을 ResponseEntity < byte []>로 변경하십시오.

+0

제안 사항은 없습니다 ...하지만 고맙습니다. – Oni1