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. MySQL中死锁(转)

    add by zhj: 总结一下,MySQL有主动和被动两种方式检测死锁. 主动方式:检查锁等待的图,如果有环,那就有死锁,这种情况下,会回滚事务. 被动方式:等待锁超时(即innodb_lock_w ...

  2. MySQL加锁处理分析(转)

    add by zhj: 非常棒的一篇文章,是我见过的讲加锁最棒最详细的文章了.之前听过网易的<MySQL微专业>,里面的课程讲的也很好,但锁这块讲的跟 这篇文章相比,还是有差距的.网易&l ...

  3. LED客显的类

    using System; using System.IO.Ports; namespace Common { public class LedHelper { ; private static st ...

  4. python脚本获取文件的创建于修改日期并计算时间差

    由于在计算一个算法的运行时间的时候,需要将文件的创建日期与修改日期读取到,然后计算两者之差,在网上搜索了相关的程序之后,自己又修改了一下,把代码贴在这里,供以后查阅使用,也希望可以帮到其他人. # - ...

  5. zabbix准备:mysql安装

    php在编译时需要mysql的配置,这样PHP远程连接mysql才有用.1.创建mysql用户和相关目录(配置文件里设置的目录) groupadd mysql useradd -g mysql -M ...

  6. WebSocket 的鉴权授权方案

    引子 WebSocket 是个好东西,为我们提供了便捷且实时的通讯能力.然而,对于 WebSocket 客户端的鉴权,协议的 RFC 是这么说的: This protocol doesn’t pres ...

  7. what's the python之模块

    正则表达式 首先,我们引入了正则表达式的知识.所谓正则表达式,就是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对 ...

  8. kafka1 三种模式安装

    一 搭建单节点单broker的kafka集群 注意:请打开不同的终端分别执行以下步骤 1.复制安装包到/usr/local目录下,解压缩,重命名(或者软链接),配置环境变量 [root@hadoop ...

  9. 005-docker-镜像使用、拉取、运行、创建、打tag

    当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载. 1.列出所有本地镜像 docker images ...

  10. webpack打包多个入口文件

    打包后的目录结构: webpack.config.js // path 模块提供了一些用于处理文件路径 const path = require('path'); // fs模块用于对系统文件及目录进 ...