[英]How to download pdf file using Feign Client?
In our project we are using feign client to make a call to third party service.在我们的项目中,我们使用 feign client 调用第三方服务。 For content type application/json it's working fine.
对于内容类型 application/json,它工作正常。 But we have a requirement where a third party service URL return pdf file and that time we are getting exception.
但是我们有一个要求,第三方服务 URL 返回 pdf 文件,而那个时候我们遇到了异常。
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.由于安全原因,我无法粘贴日志和代码,但如果有人与我分享从 feign 客户端下载 pdf 文件的代码,那将对我非常有帮助。
Thanks in advance!!提前致谢!!
You could use byte[]
as return type.您可以使用
byte[]
作为返回类型。
@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)或者提供一个端点来下载文件(Spring boot 可以返回几乎任何东西而无需使用任何外部库)
@GetMapping("/pdf")
ResponseEntity getPDF() {
byte[] resource = SomeFeignClient.getPDF();
return ResponseEntity.ok()
.contentLength(resource.length)
.contentType(MediaType.APPLICATION_PDF)
.body(resource);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.