原文: https://howtodoinjava.com/resteasy/resteasy-jboss-7-hello-world-application/
RESTEasy 是 JBOSS 提供的 JAX-RS 规范的实现,用于构建 RESTful Web 服务 和 RESTful Java 应用。 尽管这不仅限于仅在 JBOSS 中使用,您还可以与其他服务器协作。 在本文中,我将在 JBOSS AS7 服务器中构建这样的 HelloWorld 应用。
使用的环境:
- RESTEasy 2.3.1.GA
- Jboss AS7
- JDK 1.6
请按照以下步骤构建演示应用。
1)创建一个 Maven 项目并转换为 Eclipse Web 项目
mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
mvn eclipse:eclipse -Dwtpversion=2.0
2)更新pom.xml
中的运行时依赖项
您只需要定义编译时间相关性。 更好的是,如果您可以识别并包含 jboss 分发包中的 jar。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava</groupId>
<artifactId>RESTfulDemoApplication</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulDemoApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- core library -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
<scope>compile</scope>
</dependency>
<!-- JAXB support -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.3.1.GA</version>
<scope>compile</scope>
</dependency>
<!-- multipart/form-data and multipart/mixed support -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>2.3.1.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.scannotation</groupId>
<artifactId>scannotation</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>RESTfulDemoApplication</finalName>
</build>
</project>
3)创建一个空白的web.xml
文件
JBOSS 的内置支持 RESTeasy 使它成为 RESTFul Web 应用的完美组合。 构建此类应用的最低配置为无。 是的,一个空白的web.xml
文件。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Restful Web Application</display-name>
</web-app>
5)注册应用路径
您将需要扩展javax.ws.rs.core.Application
类并提供@ApplicationPath
注解。
<img src="//howtodoinjava.com/wp-content/uploads/jboss+resteasy1.png" alt="JBOSS 7+ RESTEasy demo application" width="453" height="225" class="size-full wp-image-2033" /> JBOSS 7+ RESTEasy demo applicationpackage com.howtodoinjava;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.howtodoinjava.service.UserService;
@ApplicationPath("/")
public class ApplicationConfig extends Application {
@SuppressWarnings("unchecked")
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(UserService.class));
}
}
4)编写具有@Path
注解的服务类
package com.howtodoinjava.service;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.howtodoinjava.model.User;
import com.howtodoinjava.model.Users;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "user-management")
@Path("/user-management")
public class UserService {
@XmlElement(name = "users")
private String uri1 = "/user-management/users";
@XmlElement(name = "report")
private String uri2 = "/user-managemet/generate-report";
public String getUri1() {
return uri1;
}
public void setUri1(String uri1) {
this.uri1 = uri1;
}
public String getUri2() {
return uri2;
}
public void setUri2(String uri2) {
this.uri2 = uri2;
}
@GET
@Path("/")
@Produces("application/vnd.com.demo.user-management+xml;charset=UTF-8;version=1")
public UserService getServiceInfo() {
return new UserService();
}
@GET
@Path("/users")
@Produces("application/vnd.com.demo.user-management.users+xml;charset=UTF-8;version=1")
public Users getAllUsers() {
User user1 = new User();
user1.setId(1);
user1.setFirstName("demo");
user1.setLastName("user");
user1.setUri("/user-management/users/1");
User user2 = new User();
user2.setId(2);
user2.setFirstName("demo");
user2.setLastName("user");
user2.setUri("/user-management/users/2");
Users users = new Users();
users.setUsers(new ArrayList<User>());
users.getUsers().add(user1);
users.getUsers().add(user2);
return users;
}
@GET
@Path("/users/{id}")
@Produces("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
public User getUserById(@PathParam("id") int id) {
User user = new User();
user.setId(id);
user.setFirstName("demo");
user.setLastName("user");
user.setUri("/user-management/users/" + id);
return user;
}
@POST
@Path("/users")
@Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
public Response createUser(User user,
@DefaultValue("false") @QueryParam("allow-admin") boolean allowAdmin)
throws URISyntaxException {
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
return Response.status(201)
.contentLocation(new URI("/user-management/users/123")).build();
}
@PUT
// @Path("/users/{id: [0-9]*}")
@Path("/users/{id}")
@Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
@Produces("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
public User updateUser(@PathParam("id") int id, User user)
throws URISyntaxException {
user.setId(id);
user.setFirstName(user.getFirstName() + "updated");
return user;
}
@DELETE
@Path("/users/{id}")
public Response deleteUser(@PathParam("id") int id)
throws URISyntaxException {
return Response.status(200).build();
}
}
5)运行应用
当我们在 jboss 中部署以上构建的应用并单击 URL:“http://localhost:8080/RESTfulDemoApplication/user-management/users
”时,以下是响应。
JBOSS 7+ RESTEasy 示例应用
要下载以上示例的源代码,请单击下面的链接。
祝您学习愉快!