0

I have two Rest Webservice applications where WS1 is calling WS2 to get the PDF file in byte[] encoded format as given below. The WS1 application needs to get the actual decoded byte[] from response of WS2. Can some one please help me to get the required byte[].

Method 2 in Webservice 2 to return pdf in encoded byte format:

byte [] byteArr = // PDF file converted to byte Array

return Response.ok(byteArray, MediaType.APPLICATION_OCTET_STREAM)
          .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional
          .build();

Method 1 in Webservice 1 to call method 2 in WS2 and procees and upload byte Array[PDF] to Server

ResponseEntity<String> response = //Rest call to method 2 in WS app 2
byte[] pdfByte = ??

//Code to process Byte Array
user3244519
  • 661
  • 5
  • 18
  • 36
  • if you only send the file you could look at this answer: https://stackoverflow.com/questions/40427052/spring-rest-template-for-byte – benebo22 Aug 09 '21 at 10:24
  • It sounds like WS2 is generating the Response, and WS1 is supposed to receive it: is that correct? – JohnK Jun 20 '22 at 19:29

1 Answers1

0

This code will get the response entity to a byte array. Whether it's the most efficient or not is another question.

import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.ByteArrayOutputStream;

Response resp = <call to WS2>
StreamingOutput stream = (StreamingOutput) response.getEntity();

ByteArrayOutputStream output = new ByteArrayOutputStream();
stream.write(output);
byte[] out = output.toByteArray();
JohnK
  • 6,865
  • 8
  • 49
  • 75