Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download pdf file using Feign Client?

Tags:

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!!

like image 286
abhishek chauhan Avatar asked Mar 26 '19 11:03

abhishek chauhan


1 Answers

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);
}
like image 75
V. Lovato Avatar answered Sep 20 '22 18:09

V. Lovato