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. 微信小程序tabbar设置样式在哪里改

    微信小程序tabbar通俗点说就是底部导航,我们一般会配置相关的菜单,方便读者快速导航.tabbar是在项目根目录中的配置文件 app.json 中进行设置:如果小程序是一个多 tab 应用(客户端窗 ...

  2. Spark:java.net.BindException: Address already in use: Service 'SparkUI' failed after 16 retries!

    Spark多任务提交运行时候报错. java.net.BindException: Address already retries! at sun.nio.ch.Net.bind0(Native Me ...

  3. distpicker省市区插件初始化选中值的问题

    $('#distpicker1').distpicker('destroy')  //当需要重新生成的时候,需要先销毁 $('#distpicker1').distpicker({ province: ...

  4. 论文阅读-使用隐马模型进行NER

    Named Entity Recognition in Biomedical Texts using an HMM Model  2004年,引用79 1.摘要 Although there exis ...

  5. git push 报错:missing Change-Id in commit message footer

    使用gerrit后,提交代码会出现如下截图问题: 临时解决: step1:把上面红色的那条gitidir复制下来执行下: step2:执行下面的命令会添加change_id git commit -- ...

  6. Linux学习笔记:常用100条命令(一)

    linux常用命令 1.关机 shutdown -h now 立刻关机 poweroff shutdown -r now 立刻重启 reboot logout 注销 2.进入图形界面 startx 3 ...

  7. 要求根据RandomStr.java:使用类型转换生成六位验证字符串,示例程序每次运 行时,都会生成不同的字符串。

    1.程序设计思想验证码 ①定义一个字符串变量来保存随机生成的. ②利用循环产生六位随机数,在产生每一位时将其转换为char类型并写在字符串后面. ③利用对话框显示生成的验证码,并提示用户输入验证码. ...

  8. css3--之HSL颜色

    jQuery之家: CSS3中使用的HSL颜色指南:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201503281590.html 要理解HSL ...

  9. Redis:redis.conf配置

    redis.conf配置: 配置主要分为几类:基础.快照.复制.安全.限制.详细日志.虚拟内存.高级配置.文件包含 ##------------------------------------基础配置 ...

  10. sqli-labs(十七)

    第五十四关: 这关大概意思就是尝试次数不能多于十次,必须十次之类查询处特点的key. 第一次:输入单引号报错 第二次:输入双引号不报错 说明后台是单引号进行的拼凑 第三步:这里应该是判断列,用orde ...