Skip to content
Advertisement

stream is closed and Could not find acceptable representation error

i’m consuming a Api witch is returning pdf as ResponseEntity but it is not working at my api

i’m trying to get de file coming from the external api and provide it at my endpoint.

# External Api FeignClient class

@FeignClient(value = "esatCalculoFeignClient", url = "${api.url.esat}")
public interface EsatCalculoFeignClient {

    @PostMapping("/pedido/gerar")
    ResponseEntity<InputStreamResource> gerarPdfDamEsat(@Validated @RequestBody EsatCalculoTaxasRequestDto dto);

}

My service method

//geração de DAM
public InputStreamResource solicitaDamEsat(SolicitacaDamRequestDto dto) throws BusinessException, IOException {
    FormSolicitacaoAlvara entity = this.buscaEntidadePorProtocolo(dto.getProtocolo());
    EsatCalculoTaxasRequestDto esatRequestDamDto = EsatCalculoTaxasRequestDto.toEsatDto(entity,dto.getIdMeioDePagamento(), dto.getQtdParcelas(), "123teste");
    ResponseEntity<InputStreamResource> file = esatCalculoFeignClient
            .gerarPdfDamEsat(esatRequestDamDto);

        InputStreamResource isr = new InputStreamResource(file.getBody().getInputStream());
        return isr;

 
}

my endpoint

@RequestMapping(value = "/imprimir",method = RequestMethod.GET, produces = "application/pdf")
public ResponseEntity<InputStreamResource> gerarDam(SolicitacaDamRequestDto dto) throws BusinessException, IOException {
    InputStreamResource file = service.solicitaDamEsat(dto);
    
    return ResponseEntity.ok()
            .contentType(MediaType.parseMediaType("application/pdf"))
            .contentLength(file.contentLength())
            .header(HttpHeaders.CONTENT_DISPOSITION,"attachment: filename="+file.getFilename())
            .body(file);
}

and stackTrace

enter image description here

i’m trying to get de file coming from the external api and provide it at my endpoint.

Advertisement

Answer

Replace InputStreamResouce with byte[] in Feign

@FeignClient(value = "esatCalculoFeignClient", url = "${api.url.esat}")
public interface EsatCalculoFeignClient {
    
    @PostMapping("/pedido/gerar")
    byte gerarPdfDamEsat(@Validated @RequestBody EsatCalculoTaxasRequestDto dto);

}

and convert byte[] to InputStreamResource again in service

InputStreamResource isr = new InputStreamResource(new ByteArrayInputStream(byte));
ResponseEntity<InputStreamResource> responseEntity = ResponseEntity.ok()
                        .contentType(MediaType.parseMediaType("application/pdf"))
                        .header(HttpHeaders.CONTENT_DISPOSITION,"attachment: filename=file.pdf")
                        .body(isr);
Advertisement