Serialize a java.io.File within a JSON w/ Jackson -


i've got web service (jersey 2) returns several docx , pdf files. don't know best way that:

  1. return them octet-stream. had before implementing in 2:

        @get     @path("solicitudusuario/plantilla")     @produces(mediatype.application_octet_stream)     public response descargarplantillasolicitudusuario() throws configurationexception, urisyntaxexception, ioexception{         file plantillasolicitudusuario = this.generalservice.getplantillasolicitudusuario();         return response.ok(plantillasolicitudusuario, mediatype.application_octet_stream).header("content-disposition", "attachment; filename=\"" + plantillasolicitudusuario.getname() + "\"" ).build();     } 
  2. wrap files within json. i'm using jackson json provider. i've done far:

web service:

        @get         @path("solicitudusuario/plantilla")         @produces(mediatype.application_json)         public response download() throws configurationexception, urisyntaxexception, ioexception{             file plantillasolicitudusuario = this.generalservice.getplantillasolicitudusuario();             ficherodevolver f = ficherodevolver.buildasword(plantillasolicitudusuario);             string json = mwriter.writevalueasstring(f);             return response.ok(mediatype.application_json_type.withcharset("utf-8")).entity(json).build();         } 

serializer:

        public class ficheroserializer extends jsonserializer<ficherodevolver> {          @override         public void serialize(com.ingartek.ws.zendesk_bizkaibus.model.ficherodevolver value, jsongenerator gen, serializerprovider serializers)                 throws ioexception, jsonprocessingexception {             inputstream fis = fileutils.openinputstream(value.getfichero());              gen.usedefaultprettyprinter();             gen.writestartobject();                 gen.writefieldname("fichero");                 //gen.writebinary(fis, (int)value.getficherolength());                 gen.writerawvalue(ioutils.tostring(fis, "utf-8"));                  gen.writestringfield("nombre", value.getnombre());                  gen.writestringfield("mimetype", value.getmimetype());             gen.writeendobject();              fis.close();         }          } 

data wrapper:

            @jsonserialize(using = ficheroserializer.class)             public class ficherodevolver {                  /*                  * atributos                  */                 private file fichero;                 private string nombre;                 private string mimetype;     

which best approach?

you can use this:

    file file = new file(file_path);     responsebuilder response = response.ok((object) file);     response.header("content-disposition",                 "attachment; filename=new-android-book.pdf");     return response.build(); 

with javax.ws.rs.core.response.responsebuilder


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -