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