Home > Software design >  stream is closed and Could not find acceptable representation error
stream is closed and Could not find acceptable representation error

Time:12-21

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.

CodePudding user response:

Replace InputStreamResouce with byte[] in Feign

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

and convert byte[] to InputStreamResource again in service

InputStreamResource isr = new InputStreamResource(new ByteArrayInputStream(byte));
  • Related