序列化一个io。文件在JSON w/ Jackson中。

时间:2022-09-08 18:05:44

I've got a web service (Jersey 2) which returns several DOCX and PDF files. I don't know which is the best way to do that:

我有一个web服务(Jersey 2),它返回多个DOCX和PDF文件。我不知道哪一个是最好的方法

  1. Return them as Octet-Stream. This is what I had before implementing it as 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 a Json. I'm using Jackson as json provider. This is what I've done so far:

    在Json中封装文件。我正在使用Jackson作为json提供者。这就是我迄今为止所做的:

Web service:

Web服务:

        @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 is the best approach?

哪种方法最好?

1 个解决方案

#1


1  

You can use something like 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

与javax.ws.rs.core.Response.ResponseBuilder

#1


1  

You can use something like 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

与javax.ws.rs.core.Response.ResponseBuilder