嗨,这是接收JSON和响应JSON的Web方法,但问题是这个web方法不接受gzip编码的请求,那么如何使它成为gzip可接受的web方法?
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/pdf2doc")
public RPDOC postDocument(DPDF dpdf){
}
DPDF是班级
public class DPDF{
public String docid;
public String pdfdata;
}
这个客户端代码使用gzip内容编码和throw exeption
String body = "{\"docid\": 1234, \"docdata\": \"test\"}";
HttpEntity httpEntity = EntityBuilder.create().setText(body)
.setContentEncoding("gzip")
.gzipCompress().build();
final String postUrl = serviceURL;
HttpUriRequest request = RequestBuilder.post(postUrl)
.setHeader(HttpHeaders.CONTENT_TYPE,
"application/json")
.setHeader(HttpHeaders.ACCEPT,
"application/json")
.setEntity(httpEntity)
.build();
String sResponse = null;
try (CloseableHttpClient httpclient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpclient.execute(request))
{
StatusLine sl = response.getStatusLine();
int statusCode = sl.getStatusCode();
System.out.println("HTTP Status: " + statusCode);
HttpEntity entity = response.getEntity();
if (entity != null)
{
sResponse = EntityUtils.toString(entity).trim();
System.out.println("HTTP Body:\n" + sResponse);
EntityUtils.consumeQuietly(entity);
}
}
catch (Exception e)
{
request.abort();
throw e;
}
}
catch (Exception e)
{
e.printStackTrace();
}