已有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(); } } }
相关文章推荐
- 基于SpringBoot的Restful接口开发
- Yii2 advanced版API接口开发 基于RESTful架构的 配置、实现、测试
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试
- 基于jersey搭建RESTful风格的WebService接口
- Yii2 advanced版API接口开发 基于RESTful架构的 配置、实现、测试
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)
- API接口开发 配置、实现、测试 Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试
- saltstack的rest接口salt-api开发―基于flask-restful
- Jersey开发Restful的文件上传接口如何传递数组参数
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试
- 使用 Node.js 开发基于 JavaScript 的 RESTful应用
- 《Web接口开发与自动化测试基于Python语言》–第3章
- 【接口开发】浅谈 SOAP Webserver 与 Restful Webserver 区别
- 基于OSGI的面向接口开发
- 开发基于CXF的 RESTful WebService web 项目 webservice发布
- Jersey开发RESTFul的附件依赖包mimepull.jar
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- [置顶] 基于SpringBoot开发一个Restful服务,实现增删改查功能
- 基于Vue2.x开发的音乐播放器app(推荐界面+懒加载+axios获取后端接口实现)