博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jersey入门——对Json的支持
阅读量:4576 次
发布时间:2019-06-08

本文共 7144 字,大约阅读时间需要 23 分钟。

Jersey rest接口对POJO的支持如下: 

package com.coshaho.learn.jersey;import java.net.URI;import javax.ws.rs.Consumes;import javax.ws.rs.POST;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.coshaho.learn.jersey.pojo.MyRequest;import com.coshaho.learn.jersey.pojo.MyResponse;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;public class MyJsonServer {    @POST    @Consumes(MediaType.APPLICATION_JSON)    @Produces(MediaType.APPLICATION_JSON)    public MyResponse query(MyRequest req)     {        System.out.println("Request parameter is " + req.getQuery() + " " + req.isFlag());        MyResponse resp = new MyResponse();        resp.setCode(0);        resp.setDes(req.getQuery());        return resp;    }        public static void main(String[] args) throws Exception    {        URI uri = UriBuilder.fromUri("http://127.0.0.1").port(10000).build();        ResourceConfig rc = new PackagesResourceConfig("com.coshaho.learn.jersey.pojo");                //参数支持json        rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);        HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);        server.start();                Thread.sleep(1000*1000);    }}package com.coshaho.learn.jersey;import javax.ws.rs.core.MediaType;import com.coshaho.learn.jersey.pojo.MyRequest;import com.coshaho.learn.jersey.pojo.MyResponse;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 MyJsonClient {    public static void main(String[] args)     {        ClientConfig cc = new DefaultClientConfig();                //参数支持json        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");                MyRequest req = new MyRequest();        req.setQuery("age");        req.setFlag(false);                ClientResponse response = resource                .accept(MediaType.APPLICATION_JSON)                .type(MediaType.APPLICATION_JSON)                .post(ClientResponse.class, req);                MyResponse resp = response.getEntity(MyResponse.class);        System.out.println("Response is " + resp.getCode() + " " + resp.getDes());    }}package com.coshaho.learn.jersey.pojo;public class MyRequest {    private String query;        private boolean flag;    public String getQuery()     {        return query;    }    public void setQuery(String query)     {        this.query = query;    }    public boolean isFlag() {        return flag;    }    public void setFlag(boolean flag) {        this.flag = flag;    }}package com.coshaho.learn.jersey.pojo;public class MyResponse {    private int code;        private String des;        public int getCode()     {        return code;    }        public void setCode(int code)     {        this.code = code;    }        public String getDes()     {        return des;    }        public void setDes(String des)     {        this.des = des;    }}

 这种方式的缺点是进行如下设置,并且与业务代码耦合

cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

1、 对于POJO对象,我们使用@XmlRootElement把其定义为xml bean,可以省略上述代码

@XmlRootElementpublic class MyRequest

2、 使用JSONObject进行参数传递,可以省略上述代码

package com.coshaho.learn.jersey;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 MyJsonServer {    @POST    @Consumes(MediaType.APPLICATION_JSON)    @Produces(MediaType.APPLICATION_JSON)    public JSONObject query(JSONObject query) throws JSONException     {        System.out.println(query.toString());                JSONObject resp = new JSONObject();        resp.put("code", 0);        resp.put("des", query.get("query"));        return resp;    }        public static void main(String[] args) throws Exception    {        URI uri = UriBuilder.fromUri("http://127.0.0.1").port(10000).build();        ResourceConfig rc = new PackagesResourceConfig("com");        HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);        server.start();        Thread.sleep(1000*1000);    }}package com.coshaho.learn.jersey;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 MyJsonClient {    public static void main(String[] args) throws JSONException     {        ClientConfig cc = new DefaultClientConfig();        Client client = Client.create(cc);        WebResource resource = client.resource("http://127.0.0.1:10000/query");                JSONObject req = new JSONObject();        req.put("query", "name");                ClientResponse response = resource                .accept(MediaType.APPLICATION_JSON)                .type(MediaType.APPLICATION_JSON)                .post(ClientResponse.class, req);                JSONObject resp = response.getEntity(JSONObject.class);        System.out.println(resp.toString());    }}

 3、客户端使用String方式传递参数,服务端可以把String自动转换为JSONObject,客户端也可以把JSONObject自动转换为String

package com.coshaho.learn.jersey;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.WebResource;import com.sun.jersey.api.client.config.ClientConfig;import com.sun.jersey.api.client.config.DefaultClientConfig;public class MyJsonClient {    public static void main(String[] args) throws JSONException     {        ClientConfig cc = new DefaultClientConfig();        Client client = Client.create(cc);        WebResource resource = client.resource("http://127.0.0.1:10000/query");                JSONObject req = new JSONObject();        req.put("query", "name");                String response = resource                .accept(MediaType.APPLICATION_JSON)                .type(MediaType.APPLICATION_JSON)                .post(String.class, req.toString());                System.out.println(response);    }}

 

转载于:https://www.cnblogs.com/coshaho/p/7779003.html

你可能感兴趣的文章
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
算法第5章作业
查看>>
7.9 练习
查看>>
基于ArcGIS JS API的在线专题地图实现
查看>>
learnByWork
查看>>
lua 函数
查看>>
Git的基本命令
查看>>
四平方和
查看>>
第十八周 12.27-1.2
查看>>
C# IP地址字符串和数值转换
查看>>
TCHAR和CHAR类型的互转
查看>>
常用界面布局
查看>>
C语言—— for 循环
查看>>
IBM lotus9.0测试版即将公测
查看>>
xml常用方法
查看>>
Cube Stacking(并差集深度+结点个数)
查看>>
AndroidStudio3更改包名失败
查看>>
jq 删除数组中的元素
查看>>
js URL中文传参乱码
查看>>