RESTEasy Tomcat CDI 教程
最后修改于 2023 年 1 月 10 日
RESTEasy Tomcat CDI 教程展示了如何使用 RESTEasy、Tomcat 和 CDI 创建 RESTful Web 应用程序。
RESTEasy
RESTEasy 是一个用于在 Java 中开发 RESTful Web 服务的框架。它是 JAX-RS 2.0 规范的完全认证且可移植的实现。JAX-RS 2.0 规范是一个 JCP (Java Community Process) 规范,它为通过 HTTP 协议的 RESTful Web 服务提供了一个 Java API。
RESTEasy 可以在任何 Servlet 容器中运行。它包含一套丰富的提供程序,如 XML、JSON、YAML、Fastinfoset、Multipart、XOP 和 Atom。
CDI
上下文和依赖注入 (CDI) 定义了一套强大的互补服务,有助于改进应用程序代码的结构。CDI 允许将有状态组件的生命周期和交互绑定到定义良好但可扩展的生命周期上下文,并以类型安全的方式将组件注入应用程序。CDI 的优点是:松耦合、易于测试、更好的分层、接口驱动设计推广以及动态代理。
JBoss Weld 是 CDI 规范的参考实现。
RESTEasy Tomcat CDI 示例
以下示例是一个简单的 RESTful 应用程序,它将一些与上下文相关的数据以 JSON 格式返回给客户端。该应用程序使用 Weld 并在 Tomcat 上部署。
$ tree . ├── nb-configuration.xml ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── conf │ │ │ └── AppConfig.java │ │ ├── model │ │ │ └── City.java │ │ ├── resource │ │ │ └── MyResource.java │ │ └── service │ │ ├── CityService.java │ │ └── ICityService.java │ ├── resources │ └── webapp │ ├── META-INF │ │ └── context.xml │ └── WEB-INF │ └── beans.xml └── test └── java
这是项目结构。
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>RestEasyTomcatCdi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>RestEasyTomcatCdi</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.1.4.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-servlet-initializer</artifactId> <version>3.1.4.Final</version> </dependency> <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-shaded</artifactId> <version>3.0.2.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>3.1.0.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-cdi</artifactId> <version>3.1.4.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
这是 Maven POM 文件。它包含 RESTEasy、Weld 和 Jackson 提供程序的依赖项。
<?xml version="1.0" encoding="UTF-8"?> <Context path="/RestEasyTomcatCdi"/>
在 Tomcat 的 context.xml
配置文件中,我们定义了应用的上下文路径。
<?xml version="1.0"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> </beans>
使用 CDI 的应用程序必须定义一个 beans.xml
文件。它可以是空的,就像我们的情况一样。对于 Web 应用程序,beans.xml
文件必须位于 WEB-INF
目录中。对于 EJB 模块或 JAR 文件,beans.xml
文件必须位于 META-INF
目录中。
package com.zetcode.model; import java.util.Objects; public class City { private Long id; private String name; private int population; public City() { } public City(Long id, String name, int population) { this.id = id; this.name = name; this.population = population; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } @Override public int hashCode() { int hash = 3; hash = 71 * hash + Objects.hashCode(this.id); hash = 71 * hash + Objects.hashCode(this.name); hash = 71 * hash + this.population; return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final City other = (City) obj; if (this.population != other.population) { return false; } if (!Objects.equals(this.name, other.name)) { return false; } return Objects.equals(this.id, other.id); } @Override public String toString() { return "City{" + "id=" + id + ", name=" + name + ", population=" + population + '}'; } }
这是一个 City
模型类。它包含三个属性:id
、name
和 population
。
package com.zetcode.conf; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("rest") public class AppConfig extends Application { }
这是应用程序配置类。Application
定义了 JAX-RS 应用程序的组件并提供额外的元数据。
@ApplicationPath("rest")
使用 @ApplicationPath
注解,我们设置了 RESTful Web 服务的路径。
package com.zetcode.service; import com.zetcode.model.City; import java.util.List; public interface ICityService { public List<City> findAll(); }
ICityService
包含 findAll
合同方法。
package com.zetcode.service; import com.zetcode.model.City; import java.util.ArrayList; import java.util.List; public class CityService implements ICityService { @Override public List<City> findAll() { List<City> cities = new ArrayList<>(); cities.add(new City(1L, "Bratislava", 432000)); cities.add(new City(2L, "Budapest", 1759000)); cities.add(new City(3L, "Prague", 1280000)); cities.add(new City(4L, "Warsaw", 1748000)); cities.add(new City(5L, "Los Angeles", 3971000)); cities.add(new City(6L, "New York", 8550000)); cities.add(new City(7L, "Edinburgh", 464000)); cities.add(new City(8L, "Berlin", 3671000)); return cities; } }
CityService
包含 findAll
方法的实现。它只是返回一个城市列表。这通常是从数据库等数据源检索的。
package com.zetcode.resource; import com.zetcode.model.City; import com.zetcode.service.ICityService; import java.util.List; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("cities") public class MyResource { @Inject private ICityService cityService; @GET @Produces(MediaType.APPLICATION_JSON) public List<City> message() { List<City> cities = cityService.findAll(); return cities; } }
这是 MyResource
类。
@Path("cities") public class MyResource {
@Path
指定资源响应的 URL。
@Inject private ICityService cityService;
使用 @Inject
注解,我们将 city service 对象注入到 cityService
字段中。
@GET @Produces(MediaType.APPLICATION_JSON) public List<City> message() { List<City> cities = cityService.findAll(); return cities; }
@GET
注释表示注释的方法响应 HTTP GET 请求。通过 @Produces
注释,我们定义该方法生成 JSON。我们调用一个服务方法并返回一个城市列表。消息体写入器将 Java 类转换为 JSON 并将其写入响应体。
$ curl localhost:8084/RestEasyTomcatCdi/rest/cities [{"id":1,"name":"Bratislava","population":432000},{"id":2,"name":"Budapest","population":1759000}, {"id":3,"name":"Prague","population":1280000},{"id":4,"name":"Warsaw","population":1748000}, {"id":5,"name":"Los Angeles","population":3971000},{"id":6,"name":"New York","population":8550000}, {"id":7,"name":"Edinburgh","population":464000},{"id":8,"name":"Berlin","population":3671000}]
在 Tomcat 上部署应用程序后,我们使用 curl
向应用程序发送 GET 请求。我们获得 JSON 数据。
在本教程中,我们使用 RESTEasy 和 Weld 创建了一个简单的 RESTFul 应用程序。该应用程序已部署在 Tomcat 上。