resteasy
在这里,我将向您展示如何使用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('')
@GET
public Response getUsersXML()
{
List<User> users = MockUserTable.getAll();
GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};
return Response.ok(ge).build();
}
@Path('{id}')
@GET
public Response getUserXMLById(@PathParam('id') Integer id) {
return Response.ok(MockUserTable.getById(id)).build();
}
@Path('')
@POST
public Response saveUser(User user) {
MockUserTable.save(user);
return Response.ok('<status>success<status>').build();
}
@Path('{id}')
@DELETE
public 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';
@Test
public 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);
}
@Test
public 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);
}
@Test
public 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);
}
@Test
public 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