什么是JAX-RS
全称Java API for RESTful Web Services,用于开发 RESTful风格的Webservice。
简介
本项目使用Apache CXF 开发RESTful风格的Web Service 服务,集成Spring和Hibernate,数据持久化到H2内存数据库。而且接口支持XML和JSON两种数据格式。
使用版本:
- JDK 11
- Apache CXF 3.4.5
- Spring 5.3.13
- jaxb2-maven-plugin 2.5.0
- cargo-maven3-plugin 1.9.8
- Hibernate 5.6.1
开发项目
首先,展示下项目依赖,pom文件的部分信息
<dependencies>
<!-- Apache CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
</dependency>
<!-- for JSON support in Apache-CXF Restful web service -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!-- Hibernate Core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
因为涉及持久化,所有需要创建实体及数据访问类,创建实体(Player.java)如下:
@Entity
@Table(name = "PLAYER")
@Data
public class Player {
@Id
@SequenceGenerator(name="player_sequence", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE , generator="player_sequence")
@Column(name = "PLAYER_ID")
private int playerId;
@Column(name= "NAME")
private String name;
@Column(name= "AGE")
private int age;
@Column(name= "MATCHES")
private int matches;
// 其他方法省略
实体主键使用序列,规定了序列的名字,后面测试脚本使用了这个序列。 为了方便,省略了数据访问类,直接使用 SessionFactory 访问数据。
下面是Web Services 接口(PlayerService.java)代码:
@Path("/players")
public interface PlayerService {
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN})
public Response create(PlayerType playerType);
@GET
@Path("/{id}")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getById(@PathParam("id") int id);
@PUT
@Path("/{id}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_FORM_URLENCODED})
public Response update(@PathParam("id") int id, PlayerType playerType);
@DELETE
@Path("/{id}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,})
@Produces({MediaType.APPLICATION_FORM_URLENCODED})
public Response delete(@PathParam("id") int id);
@GET
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getByName(@QueryParam("name") String name);
}
我们的接口支持XML和JSON,这个功能是需要配置的。在实现接口之前,我们需要定义客户端的视图类,也就接口返回的实体。当然,我们可以使用领域模型(Player)类,但不建议这样做。
创建客户端视图类,最好的做法是自动生成,我们编写xsd文件,使用jaxb2-maven-plugin创建自动生成文件,接口有变化时,修改xsd文件即可。创建xsd文件(Player.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://benchresources.in/cdm/Player" xmlns:tns="http://benchresources.in/cdm/Player" elementFormDefault="qualified">
<!-- Player 视图类 -->
<xsd:element name="PlayerType">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="playerId" type="xsd:int" />