Search code examples
javascriptjavajsonjakarta-eejersey

Jersey client side receive pojo us bytes


Is there any possibility to have a Restful method (Java EE) that returns back to the client a POJO serialized us bytes (using ByteArrayOutputStream, FlatBuffers or some other library)?

And the client to cast the bytes back to a POJO object by using jquery or javascript in general (us client side)?

What I had found until now, is receiving back a JSON/XML.


Solution

  • Directly converting POJO to bytes won't work as your client (javascript or so) won't know how to convert the bytes back to POJO. What you can do is this. Say your response is objA. The do this. Convert objA to String (JSON maybe) and then Base64 encode it to get byte[]. Put it inside a wrapper object objB. Return objB as a JSON from your service. Your client can just take that byte[] and since it knows that the byte[] is base64 it will be able to get the original information from there. May I ask the intent of doing this instead of XML/JSON?