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. CentOS 7 下安装jdk1.8(转)

    原文:https://blog.argcv.com/articles/3155.c CentOS 7下目前默认是jdk1.6和1.7.若需要更高版本的1.8,我们就需要一点额外的手段了. 首先,我们需 ...

  2. 对集合类的属性进行kvo观察

    在进行容器对象操作时,先调用下面方法通过key或者keyPath获取集合对象,然后再对容器对象进行add或remove等操作时,就会触发KVO的消息通知了. - (NSMutableArray *)m ...

  3. 常用的shell脚本(安全方向)

    更多shell脚本参考:https://blog.51cto.com/zero01/2046242 1.拒绝密码撞库攻击的ip shell脚本:实现对登录主机失败10次以上的ip进行拒绝登录 #! / ...

  4. (1.4)mysql sql mode 设置与使用

    关键词: mysql sql mode 1.查阅 mysql> mysql> show variables like 'sql_mode%';+---------------+------ ...

  5. Windows平台Mysql表名变小写的解决过程

    由于要弄某个项目的数据库设计文档,几百张表,如果弄在word文档里面一个个添加,效率比较低. 故分别使用了Mysql WorkBench(win版)和Powerdesigner 15来做这个设计文档. ...

  6. Cell complex单元复合形

    概念 (1)Piecewise linear complex (PLC) 分段线性复合形 (2)Cell complex 单元复合形 [1] (元胞复合形) (3)Linear Cell Comple ...

  7. [py]python的time和datetime模块获取星期几

    import time import datetime #今天星期几 today=int(time.strftime("%w")) print today #某个日期星期几 any ...

  8. Laravel中路由怎么写(一)

    1.路由基本使用示例 1.1 默认示例 Laravel中所有路由定义在/app/Http/routes.php文件中,该文件默认定义了应用的首页路由: Route::get('/', function ...

  9. Linux系统文件目录

    下面的示例是我个人的亚马逊云服务器,查看了几个关键目录的文件信息. 可以注册一个账号,免费试用一年,自己用来玩,还是很不错的 .https://aws.amazon.com/cn/ec2/ 根目录下面 ...

  10. git安装和使用(二)

    一.git安装 1.目的 通过git管理github托管项目代码 2.下载安装 Git - Downloading Package​www.git-scm.com 点击桌面,右击鼠标,出现两个git单 ...