0

I need to return an object with byte[] . My return object looks like this :

class FileInfo {

    String name;
    byte[] fileContent;
    boolean signRequired;

}

I need to return this object through a rest call. MediaType octet_stream is not suitable since my object FileInfo has both byte[] and other params. I would not want byte[] to be encoded to Base64 as it requires more work . Is there any other way to achieve this ? I saw references to mutipart data . But was not sure how to accomplish this

Thanks

Mike
  • 4,722
  • 1
  • 27
  • 40
stackuser
  • 4,081
  • 4
  • 21
  • 27
  • Without converting the byte[] to base64, I don't think it's possible. If you do decide to use base64, you can use an XmlAdapter like in [this answer](https://stackoverflow.com/a/46300638/2587435) – Paul Samsotha Nov 03 '18 at 05:24

1 Answers1

0

you should be able to return a byte [] as part of your object just like returning any other object. Your object there looks fine, maybe Im not understanding your problem.

you can have an endpoint in your service like so: (PS I use Jax.ws.rs for my rest services)

@GET
@Path("/getFileInfo")
@ApiOperation(value = "refreshPage", tags = {"v1"})
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public FileInfo getFileInfo(){

    FileInfo fileInfo = new FileInfo();
    //your stuff goes here. Get the File Info.

    return fileInfo;

}
Josh
  • 115
  • 1
  • 15
  • HI Josh,Thanks for your response. The byte[] would it be transmitted as a stream object or converted to Base64 string and transmitted? How could I indicate the stream is of type byte[] ? – stackuser Nov 02 '18 at 20:57
  • Personally, I would go with the 2nd option. I would encode the byte[] in base64 and trasnmit it that way. String fileContentAsString = Base64.getEncoder().encode(fileContent); – Josh Nov 05 '18 at 13:56
  • Thanks Josh.. I read something about Mediatype.Multipart . I posted this https://stackoverflow.com/questions/53156815/rest-can-mediatype-multipart-form-data-be-used-with-produces to see if it could be done – stackuser Nov 05 '18 at 14:55