Reputation: 5
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
i'm trying to get de file coming from the external api and provide it at my endpoint.
Upvotes: -1
Views: 312
Reputation: 1
Realising I'm a tad late to the party here but for anyone else who wanted to use Inputstreams rather than a byte array, I've found using a Resource rather than InputStreamResource works rather well and you can still get the InputStream from it:
@FeignClient(value = "esatCalculoFeignClient", url = "${api.url.esat}")
public interface EsatCalculoFeignClient {
@PostMapping("/pedido/gerar")
ResponseEntity<Resource> gerarPdfDamEsat(@Validated @RequestBody EsatCalculoTaxasRequestDto dto);
}
Upvotes: 0
Reputation: 36
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);
Upvotes: 0