一 搭建环境
1、引入包
jersey所需要的开发包下载:https://jersey.java.net/,
解压jaxrs-ri-2.26.zip文件,得到以下目录:
导入工程的lib目录:
如果有报错,根据日志信息到jaxrs-ri-2.26.zip解压后目录下查找对应包导入
2、配置web.xml
<!-- REST配置 -->
<servlet>
<servlet-name>jerseyServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.vas.appmng.pm.guangdong.odc.service.RestApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- REST配置 结束 -->
二 代码分析
1、编写web.xml配置中的服务路径类
package com.vas.appmng.pm.guangdong.odc.service;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/")
public class RestApp extends ResourceConfig {
public RestApp(){
// 服务类所在的包路径
packages("com.vas.appmng.pm.guangdong.odc.service");
}
}
2、编写服务类HelloServiceTest
package com.vas.appmng.pm.guangdong.odc.service;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import com.alibaba.fastjson.JSON;
@Path("/hello")
public class HelloServiceTest {
@GET
@Path("/index")
@Produces(MediaType.APPLICATION_JSON)
public String hello() {
Map m = new HashMap();
m.put("hello","world");
m.put("hello2","world");
return JSON.toJSONString(m);
}
@GET
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String post(@QueryParam("test")String test) {
Map<String,String> m = new HashMap<String,String>();
m.put("test",test);
m.put("hello2","world");
return JSON.toJSONString(m);
}
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String doPost(String entity) {
Map<String,String> m = new HashMap<String,String>();
m.put("test",entity);
m.put("hello2","world");
return JSON.toJSONString(m);
}
}
3、编写客户端类HelloClientTest
package com.vas.appmng.pm.guangdong.odc.service;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class HelloClientTest {
public static void main(String[] args) {
try {
Client client = ClientBuilder.newClient();
String type = "post";
WebTarget target = null;
Response res = null;
if("post".equals(type)) {
target = client
.target("http://localhost:8080/vas_mng/service/hello")
.path("/post");
requestMessage = "{'appId': 'channel01','sendTime': '2016-10-12 16:30:00','appSecret': 'e6d3e6d5db0b83498e516ce5bbd98751','version': '2.0','data': {'type': '0','size': '0'}}";
Entity entity = Entity.entity(requestMessage, MediaType.APPLICATION_JSON_TYPE);
res = target.request().accept("application/json").post(entity);
} else {
target = client
.target("http://localhost:8080/vas_mng/service/hello")
.path("/post").queryParam("test", 1111);
res = target.request().accept("application/json").get();
}
if (res.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ res.getStatus());
}
String output = res.readEntity(String.class);
System.out.println(res.getStatus() + "\n……Output from Server……");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}