Jersey提供3种基本方式来使用JSON格式
无论使用何种方式,在原有包的基础上,都需要在客户端和服务端Maven配置文件中添加jersey-json包以支持JSON格式
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-json</artifactId>
- <version>1.18</version>
- </dependency>
一,基于POJO
Request类和Response类(服务端和客户端都需要)都是基本的POJO:
- package com.sean;
- public class Request {
- private String query;
- public String getQuery() {
- return query;
- }
- public void setQuery(String query) {
- this.query = query;
- }
- }
- package com.sean;
- public class Response {
- private int respCode;
- private String respDesc;
- public int getRespCode() {
- return respCode;
- }
- public void setRespCode(int respCode) {
- this.respCode = respCode;
- }
- public String getRespDesc() {
- return respDesc;
- }
- public void setRespDesc(String respDesc) {
- this.respDesc = respDesc;
- }
- }
服务端代码:
- package com.sean;
- import java.io.IOException;
- import java.net.URI;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.UriBuilder;
- import org.glassfish.grizzly.http.server.HttpServer;
- import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
- import com.sun.jersey.api.core.PackagesResourceConfig;
- import com.sun.jersey.api.core.ResourceConfig;
- import com.sun.jersey.api.json.JSONConfiguration;
- @Path("query")
- public class MyResource {
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response query(Request req) {
- System.out.println(req.getQuery());
- Response resp = new Response();
- resp.setRespCode(0);
- resp.setRespDesc(req.getQuery());
- return resp;
- }
- public static void main(String[] args) {
- URI uri = UriBuilder.fromUri("http://127.0.0.1").port(10000).build();
- ResourceConfig rc = new PackagesResourceConfig("com.sean");
- //使用Jersey对POJO的支持,必须设置为true
- rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
- try {
- HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);
- server.start();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (NullPointerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- Thread.sleep(1000*1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
客户端代码:
- package com.sean;
- import javax.ws.rs.core.MediaType;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.WebResource;
- import com.sun.jersey.api.client.config.ClientConfig;
- import com.sun.jersey.api.client.config.DefaultClientConfig;
- import com.sun.jersey.api.json.JSONConfiguration;
- public class JerseyClient {
- public static void main(String[] args) {
- ClientConfig cc = new DefaultClientConfig();
- //使用Jersey对POJO的支持,必须设置为true
- cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
- Client client = Client.create(cc);
- WebResource resource = client.resource("http://127.0.0.1:10000/query");
- Request req = new Request();
- req.setQuery("name");
- ClientResponse response = resource
- .accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, req);
- Response resp = response.getEntity(Response.class);
- System.out.println(resp.getRespCode() + " " + resp.getRespDesc());
- }
- }
二,基于JAXB
使用JAXB的优点在于,无论使用XML格式还是JSON格式数据,都可以使用统一的JAVA模型
缺点很难找到一个合适的方式来生成特殊的JSON格式,这也是Jersey提供很多控制选项的原因
将Request类和Response类进行修改:
- package com.sean;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class Request {
- private String query;
- public String getQuery() {
- return query;
- }
- public void setQuery(String query) {
- this.query = query;
- }
- }
- package com.sean;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class Response {
- private int respCode;
- private String respDesc;
- public int getRespCode() {
- return respCode;
- }
- public void setRespCode(int respCode) {
- this.respCode = respCode;
- }
- public String getRespDesc() {
- return respDesc;
- }
- public void setRespDesc(String respDesc) {
- this.respDesc = respDesc;
- }
- }
服务端代码去掉下面的配置
- // rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
客户端代码去掉下面的配置
- // cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Jersey提供很多控制选项以便更精细的控制JSON的解析、组装过程,但是就我个人来看,JAXB提供的标签足够使用了
三,基于底层JSONObject/JSONArray
最大的优势在于可以完全控制JSON的解析、组装过程,相应的,在处理数据对象时也要更复杂
服务端代码如下:
- package com.sean;
- import java.io.IOException;
- import java.net.URI;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.UriBuilder;
- import org.codehaus.jettison.json.JSONException;
- import org.codehaus.jettison.json.JSONObject;
- import org.glassfish.grizzly.http.server.HttpServer;
- import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
- import com.sun.jersey.api.core.PackagesResourceConfig;
- import com.sun.jersey.api.core.ResourceConfig;
- @Path("query")
- public class MyResource {
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public JSONObject query(JSONObject query) {
- //{"query":"name"}
- System.out.println(query.toString());
- JSONObject resp = new JSONObject();
- try {
- resp.put("respCode", 0);
- resp.put("respDesc", query.get("query"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return resp;
- }
- public static void main(String[] args) {
- URI uri = UriBuilder.fromUri("http://127.0.0.1").port(10000).build();
- ResourceConfig rc = new PackagesResourceConfig("com.sean");
- try {
- HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);
- server.start();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (NullPointerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- Thread.sleep(1000*1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
客户端代码如下:
- package com.sean;
- import javax.ws.rs.core.MediaType;
- import org.codehaus.jettison.json.JSONException;
- import org.codehaus.jettison.json.JSONObject;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.WebResource;
- import com.sun.jersey.api.client.config.ClientConfig;
- import com.sun.jersey.api.client.config.DefaultClientConfig;
- public class JerseyClient {
- public static void main(String[] args) {
- ClientConfig cc = new DefaultClientConfig();
- Client client = Client.create(cc);
- WebResource resource = client.resource("http://127.0.0.1:10000/query");
- JSONObject req = new JSONObject();
- try {
- req.put("query", "name");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- ClientResponse response = resource
- .accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, req);
- JSONObject resp = response.getEntity(JSONObject.class);
- //{"respCode":0,"respDesc":"name"}
- System.out.println(resp.toString());
- }
- }
与JAXB相比,结果是相同的,但是处理过程(主要是组装JSON对象)要复杂
对于上面3种方式,均可使用String类代替Request类、Response类或JSONObject类,Jersey会自动将对象转换为JSON串
当然,如果客户端修改为String,服务端也要相应的修改为String类型
修改客户端代码:
- public class JerseyClient {
- public static void main(String[] args) {
- ClientConfig cc = new DefaultClientConfig();
- Client client = Client.create(cc);
- WebResource resource = client.resource("http://127.0.0.1:10000/query");
- JSONObject req = new JSONObject();
- try {
- req.put("query", "name");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- String response = resource
- .accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .post(String.class, req.toString());
- }
- }