In our project we are using feign client to make a call to third party service. For content type application/json it’s working fine. But we have a requirement where a third party service URL return pdf file and that time we are getting exception.
Due to security reason I can not paste the logs and code but if any one share me the code to download a pdf file from feign client that would be very helpful to me.
Thanks in advance!!
You could use byte[]
as return type.
@FeignClient(url = "url", name = "name")
public interface SomeFeignClient {
@GetMapping("/give-me-a-pdf")
byte[] getPDF();
}
Your service would simply call
public byte[] getPDF() {
return SomeFeignClient.getPDF();
}
Now with the bytes array you could perform any operation you want, for example saving the file using
FileUtils.writeByteArrayToFile(new File("pathname"), resource);
or provide an endpoint to download the file (Spring boot can return pretty much anything without use of any external library)
@GetMapping("/pdf")
ResponseEntity getPDF() {
byte[] resource = SomeFeignClient.getPDF();
return ResponseEntity.ok()
.contentLength(resource.length)
.contentType(MediaType.APPLICATION_PDF)
.body(resource);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With