java - Returning custom object with InputStream parameter in JAX-RS -


i'm storing objects of type binary in database , have jax-rs web service can retrieve them id.

public class binary {     private inputstream data;     private string id;     private string name;     private string description;      ... // constructors/getters/setters } 

i able working code:

@get @path("{id}") @produces(mediatype.multipart_form_data) response getbinary(@pathparam("id") string id) {     binary binary = ... // binary database     formdatamultipart multipart = new formdatamultipart();     multipart.field("name", binary.getname());     multipart.field("description", binary.getdescription());     multipart.field("data", app.getdata(),      mediatype.application_octet_stream_type);      return multipart; } 

i don't wrapping values in formdatamultipart , unwrapping them in client code. want directly return binary object this:

@get @path("{id}") @produces(/* ? */) binary getbinary(@pathparam("id") string id) {     binary binary = ... // binary database      return binary; } 

i can't use xml or json representation because of inputstream parameter. i'd appreciate of how deal problem. thanks!

if have data inputstream have problems having reset every time read inputstream. better have byte[].

if using jackson can return like:

@get @path("{id}") @produces(/* ? */) public response get(string documentid) {     binary binary = ... // binary database     return response.ok(binary).build(); } 

you can test with:

closeablehttpclient httpclient = httpclients.createdefault(); objectmapper mapper = new objectmapper(); testobj obj = new testobj(); obj.setfile(ioutils.tobytearray(new fileinputstream(new file("c:\\download.jpg")))); obj.setmimetype("image/jpeg"); obj.setdescription("asd"); string jsoninstring = mapper.writevalueasstring(obj); httppost httppost = new httppost("http://localhost:8080/url"); httppost.setheader("authorization", "bearer asdf"); httppost.setheader("content-type", "application/json"); stringentity se = new stringentity(jsoninstring); httppost.setentity(se); system.out.println(httppost.tostring()); closeablehttpresponse response2 = httpclient.execute(httppost); try {     system.out.println("!!!! " + jsoninstring);     system.out.println("!!!! " + se.tostring());     system.out.println("!!!! " + response2.getstatusline());     httpentity entity2 = response2.getentity();     entityutils.consume(entity2); } {     response2.close(); } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -