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. apache在mac下403问题

    症状:在mac下使用xampp环境,httpd.conf权限,但访问网站报403错误. 解决方案:将对应文件目录权限改为755 修改后,Thinkphp访问提示Application不可写,无法创建内 ...

  2. Linux ethtool 命令

    ethtool 是用于查询及设置网卡参数的命令,常见用法如下: 注意:该命令只是临时设置,如果网卡重启就失效了,如果想要永久保存应该配置 /etc/sysconfig/network-scripts/ ...

  3. 火币网API文档——REST 行情、交易API简介

    REST API 简介 火币为用户提供了一套全新的API,可以帮用户快速接入火币PRO站及HADAX站的交易系统,实现程序化交易. 访问地址 适用站点 适用功能 适用交易对 https://api.h ...

  4. Linux软件包的安装(rpm+yum)

    概述: 1.rpm软件包管理命令软件包的获取a.光盘镜像中有很多软件包可以使用:先挂载光盘,再查看软件包b.从软件的官网获取 .rpm 安装rpm包 ipm -ivh 软件包名称删除rpm包 ipm ...

  5. jQuery-velocity.js 插件的简易使用

    初识Velocity动画,感觉它并没有那么强大,但是渐渐感觉它的ui动画可以让我们简易的使用到我们的项目中. Velocity动画的简介: 下载地址:http://www.julian.com/res ...

  6. Ext.define细节分析

    自己写的其实还是不懂,再看看别人写的吧Extjs4 源码分析系列一 类的创建过程https://www.cnblogs.com/creazyguagua/p/4302864.htmlhttp://ww ...

  7. 一份C++学习资源,咬牙切齿地好用呀

    多年以后,你已经是一名技术总监,有一个美丽的妻子,两个孩子:你已经拥有了现在的你想都不敢想的一切:那时,你也一定会忘记,今天这篇教程,如同一颗石子,铺就过你前进的路. 下面是我们的老师根据现有资源整理 ...

  8. Pytorch快速入门及在线体验

    本文搭配了Pytorch在线环境,可以直接在线体验. Pytorch是Facebook 的 AI 研究团队发布了一个基于 Python的科学计算包,旨在服务两类场合: 1.替代numpy发挥GPU潜能 ...

  9. 超参数调试、Batch正则化和编程框架

    1.调试处理 2.为超参数选择合适的范围 3.超参数在实践中调整:熊猫与鱼子酱 4.正则化网络的激活函数 5.将batch norm拟合进神经网络 6. 为什么Batch Norm会起作用? 7.测试 ...

  10. 关于RBAC的文章

    权限系统与RBAC模型概述 RBAC权限管理模型 摘自慕课网的RBAC