宣城做网站公司,官方网站的要素,大连宏帝建设网站,学校网站集群建设RESTEasy是JBoss / RedHat的JAX-RS实现#xff0c;内置于JBoss 6之后。 在这里#xff0c;我将向您展示如何使用RESTEasy和JBossAS7.1.1.FINAL开发一个简单的RESTful Web服务应用程序。 步骤1#xff1a;使用Maven配置RESTEasy依赖项。 project xmlnshttp:maven.apache.… RESTEasy是JBoss / RedHat的JAX-RS实现内置于JBoss 6之后。 在这里我将向您展示如何使用RESTEasy和JBossAS7.1.1.FINAL开发一个简单的RESTful Web服务应用程序。 步骤1使用Maven配置RESTEasy依赖项。 project xmlnshttp:maven.apache.orgPOM4.0.0 xmlns:xsihttp:www.w3.org2001XMLSchema-instancexsi:schemaLocationhttp:maven.apache.orgPOM4.0.0 http:maven.apache.orgmaven-v4_0_0.xsdmodelVersion4.0.0modelVersion groupIdcom.sivalabsgroupIdartifactIdresteasy-demoartifactIdversion0.1version packagingwarpackagingnameresteasy-demo Maven WebappnamebuildfinalNameresteasy-demofinalNamebuilddependenciesdependencygroupIdjunitgroupIdartifactIdjunitartifactIdversion4.8.2versionscopetestscopedependencydependencygroupIdorg.jboss.resteasygroupIdartifactIdresteasy-jaxrsartifactIdversion2.3.2.FINALversionscopeprovidedscopedependencydependencygroupIdorg.jboss.resteasygroupIdartifactIdresteasy-jaxb-providerartifactIdversion2.3.2.FINALversionscopeprovidedscopedependencydependencygroupIdorg.jboss.resteasygroupIdartifactIdjaxrs-apiartifactIdversion2.3.0.GAversionscopeprovidedscopedependencydependencygroupIdorg.apache.httpcomponentsgroupIdartifactIdhttpclientartifactIdversion4.1.2versionscopeprovidedscopedependencydependenciesproject 步骤2在web.xml中配置RESTEasy web-app xmlns:xsihttp:www.w3.org2001XMLSchema-instance xmlnshttp:java.sun.comxmlnsjavaee xmlns:webhttp:java.sun.comxmlnsjavaeeweb-app_2_5.xsd xsi:schemaLocationhttp:java.sun.comxmlnsjavaee http:java.sun.comxmlnsjavaeeweb-app_3_0.xsd idWebApp_ID version3.0listenerlistener-classorg.jboss.resteasy.plugins.server.servlet.ResteasyBootstraplistener-classlistenerservletservlet-nameResteasyservlet-nameservlet-classorg.jboss.resteasy.plugins.server.servlet.HttpServletDispatcherservlet-classservletservlet-mappingservlet-nameResteasyservlet-nameurl-patternrest*url-patternservlet-mappingcontext-paramparam-nameresteasy.servlet.mapping.prefixparam-nameparam-valuerestparam-valuecontext-paramcontext-paramparam-nameresteasy.scanparam-nameparam-valuetrueparam-valuecontext-paramweb-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;XmlRootElementXmlAccessorType(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 MapInteger, User USER_MAP new HashMapInteger, User();static{USER_MAP.put(1, new User(1,admin,admingmail.com,new Date()));USER_MAP.put(2, new User(2,test,testgmail.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 ListUser getAll(){ListUser users new ArrayListUser(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() {ListUser users MockUserTable.getAll();GenericEntityListUser ge new GenericEntityListUser(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(statussuccessstatus).build();}Path({id})DELETEpublic Response deleteUser(PathParam(id) Integer id) {MockUserTable.delete(id);return Response.ok(statussuccessstatus).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_URLusers);ClientResponseListUser response request.get(new GenericTypeListUser(){});ListUser users response.getEntity();Assert.assertNotNull(users);}Testpublic void testGetUserById() throws Exception {ClientRequest request new ClientRequest(ROOT_URLusers1);ClientResponseUser 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(user3gmail.com);ClientRequest request new ClientRequest(ROOT_URLusers);request.body(applicationxml, user);ClientResponseString response request.post(String.class);String statusXML response.getEntity();Assert.assertNotNull(statusXML);}Testpublic void testDeleteUser() throws Exception {ClientRequest request new ClientRequest(ROOT_URLusers2);ClientResponseString 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