您的位置:首页 > 其它

已有mvc框架下基于jersey的restful接口开发

2017-12-20 11:11 489 查看

一 搭建环境

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
4000
_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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jersey 框架
相关文章推荐