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教程第1部分:基础相关推荐

  1. RESTEasy教程第3部分:异常处理

    在开发软件应用程序时,异常处理是显而易见的要求. 如果在处理用户请求时发生任何错误,我们应该向用户显示一个错误页面,其中包含详细的异常消息,错误代码(可选),更正输入和重试的提示(可选)以及实际根本原 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. 解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题
  2. 再谈Linux修改应用程序获得root权限
  3. 读书笔记2013第10本:《学得少却考得好Learn More Study Less》
  4. 编程入门书籍-Python基础教程(第3版)
  5. Atitit.jpg png格式差别以及解决jpg图片不显示的问题
  6. 搭建前端私有npm杂记
  7. python基础知识选择题-99道经典练习题助你全面掌握python基础知识,附技巧答案...
  8. 机器学习(六)——优化器
  9. 《微软开源跨平台移动开发实践》团购通知
  10. 小问题,对递归重复调用的改进,一起来分享
  11. TortoiseGit 单文件版本提交记录查看_入门试炼_08
  12. 2022,这些地图可视化,够你用一整年了(附可视化素材)
  13. 在WinForm中使用Web Service来实现软件自动升级
  14. SparkSql学习笔记(包含IDEA编写的本地代码)
  15. Java中函数参数不固定的问题
  16. 工作中postgre使用过的函数。
  17. 随机一个淘宝买家秀网站源码
  18. SpringBoot+WebSocket问题:Failed to register @ServerEndpoint class
  19. 使用vs2019创建win32动态链接库
  20. 皮尔森类似度(Pearson Similiarity)计算举例与数学特性和存在问题

热门文章

  1. java的几种对象(PO,VO,DAO,BO,POJO)解释
  2. leetcode初级算法3.存在重复元素
  3. jsf el表达式_JSP,JSF和EL简介
  4. 使用Spring boot,Thymeleaf,AngularJS从零开始构建新的Web应用程序–第3部分
  5. j2ee可以用于前端开发吗_用于J2EE开发的Cloud IDE
  6. 如何将JAR添加到Jetbrains MPS项目
  7. Java命令行界面(第13部分):JArgs
  8. 使用Spring boot,Thymeleaf,AngularJS从零开始构建新的Web应用程序-第2部分
  9. 复制模式和扩展模式_扩展剂:模式还是反模式?
  10. 并发加对象锁_通用并发对象池