Jersey RESTful WebService框架是一个开源的、产品级别的JAVA框架,支持JAX-RS API并且是一个JAX-RS(JSR 311和 JSR 339)的参考实现
Jersey不仅仅是一个JAX-RS的参考实现,Jersey提供自己的API,其API继承自JAX-RS,提供更多的特性和功能以进一步简化RESTful service和客户端的开发
下面简单介绍一下其客户端、服务端的实现逻辑
客户端Test.java
package com.ardo.client;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.ardo.bean.Info;
import com.ardo.bean.Student;
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;
import com.sun.jersey.api.json.JSONConfiguration;
public class Test {
private Client client ;
/**
* 初始化jersey客户端
*/
public void init(){
if(client==null){
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
client = Client.create(config);
client.setConnectTimeout(3000);
}
}
public Info get(String url, String path, MultivaluedMap<String, String> queryParams){
try {
return queryParams == null?client.resource(url).path(path).type(MediaType.APPLICATION_JSON).get(Info.class):
client.resource(url).queryParams(queryParams).path(path).type(MediaType.APPLICATION_JSON).get(Info.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getString(String url, String path, MultivaluedMap<String, String> queryParams){
try {
return queryParams == null?client.resource(url).path(path).type(MediaType.APPLICATION_JSON).get(String.class):
client.resource(url).queryParams(queryParams).path(path).type(MediaType.APPLICATION_JSON).get(String.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 客户端请求【get方式-返回实体对象|字符串】
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
test.init();
//Info info = test.get("http://localhost:8899/", "jersey/services/hello/get/Tom", null);
//System.out.println("请求返回:"+info.getCode()+" "+info.getName());
String info = test.getString("http://localhost:8899/", "jersey/services/hello/get/Tom", null);
System.out.println("请求返回:"+info);
}
/**
* 客户端请求【post方式】
* @param argc
* @throws JSONException
*/
public static void main4(String[] argc) throws JSONException {
Client c = Client.create();
WebResource r=c.resource("http://localhost:8899/jersey/services/hello/getpost");
JSONObject obj = new JSONObject();
obj.put("name", "张晓梅2331");
obj.put("school", "华北大学2342");
JSONObject response = r.type(MediaType.APPLICATION_JSON_TYPE).post(JSONObject.class, obj);
System.out.println(response.toString());
}
}
服务端 ArdoService.java
示例代码中展示了多种方式:get、post、文本、xml、json、多路径参数、多动态参数匹配等
package com.ardo.service;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.ardo.bean.Info;
import com.ardo.bean.Student;
@Path("/hello")
public class ArdoService {
private static String serverUri = "http://localhost:8080/jerseyDemo/webapi";
@GET
@Produces( { "text/plain" })
public String getClientMessage() {
return "hello ardo jersey!";
}
/**
* http://localhost:8899/jersey/services/hello/阿杜
* @param username
* @return
*/
@GET
@Path("/{param}")
@Produces("text/plain;charset=UTF-8")
public String sayHelloToUTF8(@PathParam("param") String username) {
return "Hello " + username;
}
/**
* xml格式
* http://localhost:8899/jersey/services/hello/xml/阿杜
* @param username
* @return
*/
@GET
@Path("/xml/{param}")
@Produces({ MediaType.APPLICATION_XML })
public Info sayHelloToXml(@PathParam("param") String username) {
Info hi = null;
try {
hi = new Info();
hi.setCode("A001");
hi.setName(username);
} catch (Exception e) {
e.printStackTrace();
}
return hi;
}
/**
* json格式
* http://localhost:8899/jersey/services/hello/json/阿杜
* @param username
* @return
*/
@GET
@Path("/json/{param}")
@Produces({ "application/json;charset=UTF-8" })
public Info sayHelloToJson(@PathParam("param") String username) {
Info hi = null;
try {
hi = new Info();
hi.setCode("A001");
hi.setName(username);
} catch (Exception e) {
e.printStackTrace();
}
return hi;
}
/**
* http://localhost:8899/jersey/services/hello/ws?param=阿杜
* @param param
* @return
*/
@GET
@Path("/ws")
@Produces("text/plain;charset=UTF-8")
public String sayHis(@QueryParam("param") String param) {
return "nice " + param;
}
@GET
@Path("/get/{name}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Student getInfo(@PathParam("name") String name){
System.out.println("get服务端查询:"+name);
Student info = new Student();
info.setAge(21);
info.setSchool("理工大学");
info.setName(name);
return info;
}
@POST
@Path("/getpost")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Student getInfo2(JSONObject obj) throws JSONException{
String name = obj.get("name")+"";
String school = obj.get("school")+"";
System.out.println("post服务端查询:"+name);
Student info = new Student();
info.setAge(21);
info.setSchool(school);
info.setName(name);
return info;
}
//多个动态参数匹配
@GET
@Path("/{param1}/{param2}")
@Produces({ "application/json;charset=UTF-8" })
public String mytest(@PathParam("param1") String param1, @PathParam("param2") String param2) {
System.out.println("param1:"+param1+" param2:"+param1);
return "param1:"+param1+" param2:"+param1;
}
}