代码之家  ›  专栏  ›  技术社区  ›  krezus

如何通过应用程序/八位字节流使用jersey生成和使用rest服务?

  •  0
  • krezus  · 技术社区  · 7 年前

    首先,我需要生成rest服务来发送POJO类,包括图像的字节数组字段和其他POJO类。还需要使用jersey客户端使用此服务。使用应用程序/八位字节流媒体类型可以实现这些。我已经做了唯一的图像文件,它是工作。 正确的方法是什么?

        public class Sample{
            int               sampleId;
            Byte[]            image;
            Foo               foo;
    
           //constructor
           //getter setter
        }
    

        public class GetDataImage {
    
            @GET
            @Path("/gets")
            @Produces(MediaType.APPLICATION_OCTET_STREAM)
            public Response getFile(@QueryParam("id") String id ) throws IOException {
    
                File file = new 
                File("..\test_image.jpg");
                RenderedImage image2 = ImageIO.read(file);
    
                Foo foo = new Foo();
                Sample sample = new Sample (1, new Byte[] {},foo  );
    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectMapper mapper = new ObjectMapper(new BsonFactory());
                mapper.writeValue(baos, responseChipoutImage);
    
                   StreamingOutput stream = new StreamingOutput() {
                      @Override
                      public void write(OutputStream output) throws IOException {
                        try {
                         // ImageIO.write(image2, "jpg", output);
                            new ObjectOutputStream(output).writeObject(responseChipoutImage);
                        } catch (Exception e) {
                           e.printStackTrace();
                        }
                      }
                    };
    
                        return Response.ok(stream, "application/octet-stream") 
                                .header("content-disposition", "attachment; filename = " + image2.toString())
                                .build();
                        }
    }
    

    这是客户:

    public class Client {
    
        private static final String BASE_URI = "http://localhost:8090/Test/gets";
    
        public Client () throws IOException {
    
            try {
                Client client = Client.create();
                WebResource objWebResource = client.resource(BASE_URI);
                ClientResponse response = objWebResource.path("/").queryParam("id", "1")
                                .type(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class);
    
                System.out.println("response : " + response);
                if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
    
                ResponseSample responseSample = response.getEntity(ResponseSample.class);
    
    //          InputStream input = (InputStream)response.getEntity(InputStream.class);
    //          BufferedImage bf = ImageIO.read(input);
    //          File outputfile = new File("../test.jpeg");
    //          ImageIO.write(bf, "jpg", outputfile);           
    
                ObjectMapper mapper = new ObjectMapper(new BsonFactory());
                // deserialize data
         }
            } catch (UniformInterfaceException e) {
                e.printStackTrace();
            } catch (ClientHandlerException e) {
                e.printStackTrace();
            }
    
    
        }
    
        public static void main(String... args) throws IOException {
            new Client();
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   krezus    7 年前

    我终于找到了解决办法。解决方案隐藏在Jackson JSON解析器中--> Bson4jackson公司 .

    更改的服务器端 流量输出 ovveride方法如下:

    StreamingOutput stream = new StreamingOutput() {
                  @Override
                  public void write(OutputStream output) throws IOException {
                    try {      
                    ObjectMapper mapper = new ObjectMapper(new BsonFactory());
                    mapper.writeValue(output, responseChipoutImage);
                    } catch (Exception e) {
                       e.printStackTrace();
                    }
                  }
                };
    

    然后从添加jackson bson解析器的客户端捕获数据 输入流 .

    public class Client {
    private static final String BASE_URI = "http://localhost:8090/Test/gets";
    
    public Client() throws IOException {
    
        try {
            Client client = Client.create();
            WebResource objWebResource = client.resource(BASE_URI);
            ClientResponse response = objWebResource.path("/").queryParam("id", "1")
                .type(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class);
            if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
    
                ObjectMapper mapper = new ObjectMapper(new BsonFactory()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                ResponseSample responseSample = mapper.readValue(response.getEntityInputStream(), ResponseSample.class);
            }
        } catch (UniformInterfaceException e) {
            e.printStackTrace();
        } catch (ClientHandlerException e) {
            e.printStackTrace();
        }
    
    }
    public static void main(String...args) throws IOException {
        new Client();
    }