5
votes

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

1

1 Answers

4
votes

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);
}