• Jersey入门——对Json的支持


    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,可以省略上述代码

    @XmlRootElement
    public 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);
        }
    }
  • 相关阅读:
    HDU 2896 病毒侵袭 (AC自动机)
    HDU 2222 Keywords Search (AC自动机)
    HDU 2457 DNA repair (AC自动机+DP)
    CoFun 1612 单词分组(容斥)
    邓_mysql_面试
    Html5+js测试题(开发版)
    Html5+js测试题【完整版】
    HTML面试
    支付宝+微信=合成二维码
    邓_laravel框架——news
  • 原文地址:https://www.cnblogs.com/coshaho/p/7779003.html
Copyright © 2020-2023  润新知