如何通过application/octet_stream使用球衣生产和消费休息服务?

How to produce and consume rest service using jersey via application/octet_stream?

首先,我需要为发送 POJO 生成 rest 服务 class 包括图像和其他 POJO 的字节数组字段 class。还需要使用球衣 client.It 使用此服务,可以使用 application/octet-stream MediaType 实现这些。我已经为图像文件做了这件事并且它正在工作。 这样做的正确方法是什么?

    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();
    }

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

更改了服务器端 StreamingOutput 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();
                }
              }
            };

然后从 InputStream.

添加 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();
}