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);
}
}

Jersey入门——对Json的支持的更多相关文章

  1. Jersey框架二:Jersey对JSON的支持

    Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...

  2. MySQL 5.7原生JSON格式支持

    在MySQL与PostgreSQL的对比中,PG的JSON格式支持优势总是不断被拿来比较.其实早先MariaDB也有对非结构化的数据进行存储的方案,称为dynamic column,但是方案是通过BL ...

  3. [四]SpringMvc学习-对servlet与json的支持与实现

    1.对servletAPI的支持 request.response.session作为参数自动注入 2.对Json的支持 2.1springmvc配置文件中添加支持对象与json的转换 <mvc ...

  4. Java Restful框架:Jersey入门示例(官方例子)

    本文主要介绍了Java Restful框架Jersey入门例子(来源于官方网站https://jersey.java.net/),废话不多说进入正题. 在Jersey官方示例中(https://jer ...

  5. 字定义JSON序列化支持datetime格式序列化

    字定义JSON序列化支持datetime格式序列化 由于json.dumps无法处理datetime日期,所以可以通过自定义处理器来做扩展,如: import json from datetime i ...

  6. spring json的支持

    在spring中可以通过配置来实现对json的支持: 以下连接是看到的一篇对这方面内容讲解比较好的文章 http://www.cnblogs.com/fangjian0423/p/springMVC- ...

  7. 三、SQL Server 对JSON的支持

    一.SQL Server 对JSON的支持 一.实现效果   现在 我用数据库是sql2008 ,共计2万数据. 每一条数据里面的有一个为attribute字段是 json存储状态属性,  我怎么查看 ...

  8. Jersey 入门与Javabean

    Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service,它包含三个部分: 核心服务器(Core Server) 通过提供JSR 311中标准化的注释和AP ...

  9. SQL Server 2016 JSON原生支持实例说明

    背景 Microsoft SQL Server 对于数据平台的开发者来说越来越友好.比如已经原生支持XML很多年了,在这个趋势下,如今也能在SQLServer2016中使用内置的JSON.尤其对于一些 ...

随机推荐

  1. Lua: 给 Redis 用户的入门指导(转)

    add by zhj : Lua与Multi/EXEC的功能比较像,在执行过程中,redis不会执行其它命令,这就不会有并发访问的问题, 这是非常好的.但Multi/EXEC要求所有命令都是独立的,后 ...

  2. SpringBoot-整合lombok

    添加lombok依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lom ...

  3. 接口测试工具-Jmeter使用笔记(四:响应断言)

    Jmeter中断言的类型有许多,我不在这里一一列举,只说下我用到的---响应断言. 作用:一个HTTP请求发出去,怎么判断执行的任务是否成功呢?通过检查服务器响应数据,是否返回预期想要的数据,如果是, ...

  4. 20171018 微信小程序客户端数据和服务器交互

    -- 时常在想,怎么样才能把知识写的清晰,其实是我理解的不够清晰 微信小程序其实是一个客户端页面,也是需要和服务器交互才能体现数据. 1 --服务器搭建Web API :MVC4 中的一个模板, 如下 ...

  5. PHP做APP接口时,如何保证接口的安全性??????????

    PHP做APP接口时,如何保证接口的安全性? 1.当用户登录APP时,使用https协议调用后台相关接口,服务器端根据用户名和密码时生成一个access_key,并将access_key保存在sess ...

  6. Centos7下ups监控apcupsd的使用

    什么是UPS UPS-Uninterrupted Power System:利用电池化学能作为后备能量,在市电断电等电网故障时,不间断地为用户设备提供(交流)电能的一种能量转换装置. UPS的主要功能 ...

  7. Jquery书写AJAX动态向页面form传数据,清空之前的数据

    三种方式: 直接代码: 1.$("#mytable tr:gt(0)").remove(); 2.$("#mytable tr:not(:first)").em ...

  8. ASP.Net中的四种状态保持机制

    每个人上网可多有过这样的情况,当我们登陆某个网站时,在登陆的旁边会有一个 "记住我" 的复选框,有的网站还会让用户选择记住我.这个记住我是怎么实现的呢? 其实就用利用的是cooki ...

  9. gerrit上的commit msg中关联jira单号(含gerrit的安装)

    这个问题折腾了很久,前期后后大概一个月吧,终于搞定了,查了很多资料,有的不完整,有的完全就不能用,有的没说完整,所以一定要记录下来,我的心血啊. 由于公司在用的gerrit的是正式环境中的,大家都在使 ...

  10. node微信公众号开发--设置自定义菜单

    var request = require("request"); const querystring = require("querystring"); re ...