如何通过application / octet_stream使用平针织生产和消费休息服务?

时间:2017-11-23 07:33:09

标签: java rest jersey bson jersey-client

首先,我需要为图像和其他POJO类发送POJO类包含字节数组字段的休息服务。还需要使用jersey客户端来使用此服务。可以使用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();
    }

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。该解决方案隐藏在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();
}