resteasy

RESTEasy是来自JBoss / RedHat的JAX-RS实现,并且内置于JBoss 6之后。
在这里,我将向您展示如何使用RESTEasy和JBossAS7.1.1.FINAL开发一个简单的RESTful Web服务应用程序。
步骤#1:使用Maven配置RESTEasy依赖项。

<project xmlns='http:maven.apache.orgPOM4.0.0' xmlns:xsi='http:www.w3.org2001XMLSchema-instance'xsi:schemaLocation='http:maven.apache.orgPOM4.0.0 http:maven.apache.orgmaven-v4_0_0.xsd'><modelVersion>4.0.0<modelVersion> <groupId>com.sivalabs<groupId><artifactId>resteasy-demo<artifactId><version>0.1<version>  <packaging>war<packaging><name>resteasy-demo Maven Webapp<name><build><finalName>resteasy-demo<finalName><build><dependencies><dependency><groupId>junit<groupId><artifactId>junit<artifactId><version>4.8.2<version><scope>test<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>resteasy-jaxrs<artifactId><version>2.3.2.FINAL<version><scope>provided<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>resteasy-jaxb-provider<artifactId><version>2.3.2.FINAL<version><scope>provided<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>jaxrs-api<artifactId><version>2.3.0.GA<version><scope>provided<scope><dependency><dependency><groupId>org.apache.httpcomponents<groupId><artifactId>httpclient<artifactId><version>4.1.2<version><scope>provided<scope><dependency><dependencies><project>

步骤#2:在web.xml中配置RESTEasy

<web-app xmlns:xsi='http:www.w3.org2001XMLSchema-instance' xmlns='http:java.sun.comxmlnsjavaee' xmlns:web='http:java.sun.comxmlnsjavaeeweb-app_2_5.xsd' xsi:schemaLocation='http:java.sun.comxmlnsjavaee http:java.sun.comxmlnsjavaeeweb-app_3_0.xsd' id='WebApp_ID' version='3.0'><listener><listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap<listener-class><listener><servlet><servlet-name>Resteasy<servlet-name><servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher<servlet-class><servlet><servlet-mapping><servlet-name>Resteasy<servlet-name><url-pattern>rest*<url-pattern><servlet-mapping><context-param><param-name>resteasy.servlet.mapping.prefix<param-name><param-value>rest<param-value><context-param><context-param><param-name>resteasy.scan<param-name><param-value>true<param-value><context-param><web-app>

步骤#3:创建User域类,MockUserTable类以将User对象存储在内存中以进行测试,并创建UserResource类以将对CRUD的操作公开为RESTful Web服务。

package com.sivalabs.resteasydemo;import java.util.Date;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)public class User {private Integer id;private String name;private String email;private Date dob;setters and getters}package com.sivalabs.resteasydemo;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import com.sivalabs.resteasydemo.User;public class MockUserTable {private static Map<Integer, User> USER_MAP = new HashMap<Integer, User>();static{USER_MAP.put(1, new User(1,'admin','admin@gmail.com',new Date()));USER_MAP.put(2, new User(2,'test','test@gmail.com',new Date()));}public static void save(User user){USER_MAP.put(user.getId(), user);}public static User getById(Integer id){return USER_MAP.get(id);}public static List<User> getAll(){List<User> users = new ArrayList<User>(USER_MAP.values());return users;}public static void delete(Integer id){USER_MAP.remove(id);} }package com.sivalabs.resteasydemo;import java.util.List;import javax.ws.rs.DELETE;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.core.GenericEntity;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import com.sivalabs.resteasydemo.MockUserTable;@Path('users')@Produces(MediaType.APPLICATION_XML)public class UserResource {@Path('')@GETpublic Response getUsersXML() {List<User> users = MockUserTable.getAll();GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};return Response.ok(ge).build();}@Path('{id}')@GETpublic Response getUserXMLById(@PathParam('id') Integer id) {return Response.ok(MockUserTable.getById(id)).build();}@Path('')@POSTpublic Response saveUser(User user) {MockUserTable.save(user);return Response.ok('<status>success<status>').build();}@Path('{id}')@DELETEpublic Response deleteUser(@PathParam('id') Integer id) {MockUserTable.delete(id);return Response.ok('<status>success<status>').build();}}

步骤#6:使用JUnit TestCase测试REST Web服务。

package com.sivalabs.resteasydemo;import java.util.List;import org.jboss.resteasy.client.ClientRequest;import org.jboss.resteasy.client.ClientResponse;import org.jboss.resteasy.util.GenericType;import org.junit.Assert;import org.junit.Test;import com.sivalabs.resteasydemo.User;public class UserResourceTest {static final String ROOT_URL = 'http:localhost:8080resteasy-demorest';@Testpublic void testGetUsers() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users');ClientResponse<List<User>> response = request.get(new GenericType<List<User>>(){});List<User> users = response.getEntity();Assert.assertNotNull(users);}@Testpublic void testGetUserById() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users1');ClientResponse<User> response = request.get(User.class);User user = response.getEntity();Assert.assertNotNull(user);}@Testpublic void testSaveUser() throws Exception {User user = new User();user.setId(3);user.setName('User3');user.setEmail('user3@gmail.com');ClientRequest request = new ClientRequest(ROOT_URL+'users');request.body('applicationxml', user);ClientResponse<String> response = request.post(String.class);String statusXML = response.getEntity();Assert.assertNotNull(statusXML);}@Testpublic void testDeleteUser() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users2');ClientResponse<String> response = request.delete(String.class);String statusXML = response.getEntity();Assert.assertNotNull(statusXML);}}

步骤7:要测试REST服务,我们可以使用REST客户端工具。
您可以在http://code.google.com/a/eclipselabs.org/p/restclient-tool/下载REST客户端工具。

重要注意事项:
1.应先注册org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap监听器。

2.如果HttpServletDispatcher Servlet URL模式不是/ *,则应配置resteasy.servlet.mapping.prefix <context-param>

继续本教程的第二部分 。

参考: RESTEasy教程第1部分: JCG合作伙伴 Siva Reddy的基础知识,来自My Experiments on Technology博客。

翻译自: https://www.javacodegeeks.com/2012/06/resteasy-tutorial-part-1-basics.html

resteasy

resteasy_RESTEasy教程第1部分:基础相关推荐

  1. 【Android开发教程】一、基础概念

    Android操作系统 Android是一个基于Linux.使用java作为程序接口的操作系统.他提供了一些工具,比如编译器.调试器.还有他自己的仿真器(DVM - Dalvik Virtual Ma ...

  2. python基础教程博客_python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

  3. python基础教程攻略-python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

  4. python菜鸟基础教程-python基础菜鸟教程,Python的基础语法

    原标题:python基础菜鸟教程,Python的基础语法 什么是Python?Python是一门简单直观的编程语言,并且目前是开源的,可以方便任何人使用. Python的开发哲学:用一种方法,最好是只 ...

  5. css点击a标签显示下划线_好程序员HTML5培训教程-html和css基础知识

    好程序员HTML5培训教程-html和css基础知识,Html是超文本标记语言(英语全称:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言. Css ...

  6. Python教程分享之Python基础知识点梳理

    Python语言是入门IT行业比较快速且简单的一门编程语言,学习Python语言不仅有着非常大的发展空间,还可以有一个非常好的工作,下面小千就来给大家分享一篇Python基础知识点梳理. Python ...

  7. ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我们将介绍 A ...

  8. “.NET研究”【Android开发教程】一、基础概念

    Android操作系统 Android是一个基于Linux.使用java作为程序接口的操作系统.他提供了一些工具,比如编译器.调试器.还有他自己的仿真器(DVM - Dalvik Virtual Ma ...

  9. 计算机考试视频教程江西,江西计算机二级自学教程推荐:公共基础知识(2018年版)...

    &nbsp&nbsp[导读]: 江西计算机二级自学教程推荐:公共基础知识(2018年版),更多计算机等级考试用书.考试内容和考试模拟试题,请访问易考吧计算机等级网 江西计算机二级自学教 ...

  10. 江苏计算机一级怎么自学,江苏计算机一级自学教程推荐:计算机基础及MS Office应用上机指导(2018年版)...

    &nbsp&nbsp[导读]: 江苏计算机一级自学教程推荐:计算机基础及MS Office应用上机指导(2018年版),更多计算机等级考试用书.考试内容和考试模拟试题,请访问易考吧计算 ...

最新文章

  1. 真我新格调 勇敢使梦想×××
  2. 用于软件包管理的21个Linux YUM命令 转载
  3. javascript弹出div(一)
  4. Nginx使用brotli代替gzip
  5. Magicodes.IE 2.2里程碑需求和建议征集
  6. Linux下设置和查看环境变量
  7. java冒泡排序经典代码_15道经典Java算法题(含代码) 建议收藏
  8. 帆软FineMobile 消息推送/定时调度
  9. AutoSar和OSEK网络管理比较
  10. 离散数学之关系(传递闭包)
  11. Flask入门(三)~补充及虚拟环境
  12. 蛋白质二级、三级结构预测
  13. RAM和ROM存储空间的混合
  14. 关于springboot部署服务器的步骤
  15. 数据库修改用友U8账套
  16. 魔术轮胎,dugoff轮胎建模 采用模块化建模方法,搭建非线性魔术轮胎PAC2002,dugoff模型
  17. IMU(LPMS-B2) ROS下使用教程
  18. autocad Objectarx 使用setWindowArea设置打印区域后,发现与实际打印出来的区域不一致的问题
  19. 解决Vue.directives is not a function报错
  20. CentOS安装配置Maven

热门文章

  1. eclipse下载与安装步骤详解,包含解决错误(最全最详细)
  2. JS对象的属性名规则
  3. @ResponseBody导致的返回值中文乱码
  4. 解决高版本SpringBoot整合swagger时启动报错:Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
  5. php制作留言板的题_PHP实现留言板功能实例代码
  6. arcgis adf数据_使用ADF列表视图的主从数据
  7. swagger生成示例_生成器设计模式示例
  8. js 序列化内置对象_内置序列化技术
  9. jboss fuse 教程_JBoss Fuse –一些鲜为人知的技巧
  10. swagger api文档_带有Swagger的Spring Rest API –创建文档