2017-09-29 108 views
1

新泽西州的WebMethod接收JSON和返回JSON,我创建了一个客户端后用gzip编码的JSON,我不知道它是编码的JSON张贴正确的方式,我想确认请求正文是否真的被压缩?后gzip压缩JSON泽西服务

这是球衣的WebMethod

@POST 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
@Path("/getgzip") 
public RPGZIP postDocument(RGZIP gzip){   
    RPGZIP _response = new RPGZIP(); 
    _response.rpid = gzip.docid; 
    _response.rpdata = gzip.docdata; 
    return _response; 
} 

这是客户端类消费球衣

import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.client.Entity; 
import javax.ws.rs.core.Response; 
import com.gzip.RGZIP; 
import org.glassfish.jersey.message.GZipEncoder; 
public class rclient { 
public static void main(String[] args){ 
    String REST_URI = 
    "http://localhost:8084/TestGZip/webresources/gzip/getgzip"; 
    Client client = ClientBuilder.newClient(); 
    client.register(GZipEncoder.class); 
    RGZIP rzip = new RGZIP(); 
    rzip.docid = 1234; 
    rzip.docdata = "Working..";   
    Response r = 
      client.target(REST_URI).request(MediaType.APPLICATION_JSON) 
      .post(Entity.entity(rzip, MediaType.APPLICATION_JSON)); 
    System.out.println(r.readEntity(String.class)); 
} 
} 

回答

0

要知道,你的请求被压缩,必须有一个请求头像Content-Encoding: gzip发送到服务器服务器要知道,他们必须解压缩载荷

你的所作所为是在你的代码是正确的。

+0

我的同事试图张贴压缩JSON泽西方法,他获得套接字错误?他告诉我我应该添加并注册gzip ReaderInterceptor来使用压缩的请求? – Siva

+0

你在说“消费”还是阅读? – Optional

+0

我已经从NetBeans检查请求的头部和上述代码不设定内容编码的。如何设置内容编码? – Siva