什么是REST
REST 全稱是 Representational State Transfer(表述性狀態轉移),它是 Roy Fielding 博士在 2000 年寫的一篇關于軟體架構風格的論文,許多知名互聯網公司開始采用這種輕量級 Web 服務,大家習慣將其稱為 RESTful Web Services,或簡稱 REST 服務,
REST 本質上是使用 URL 來訪問資源的一種方式,總所周知,URL 就是我們平常使用的請求地址了,其中包括兩部分:請求方式 與 請求路徑,比較常見的請求方式是 GET 與 POST,但在 REST 中又提出了幾種其它型別的請求方式,匯總起來有六種:GET、POST、PUT、DELETE、HEAD、OPTIONS,尤其是前四種,正好與 CRUD(增刪改查)四種操作相對應:GET(查)、POST(增)、PUT(改)、DELETE(刪),
實際上,REST 是一個“無狀態”的架構模式,因為在任何時候都可以由客戶端發出請求到服務端,最侄訓傳自己想要的資料,也就是說,服務端將內部資源發布 REST 服務,客戶端通過 URL 來訪問這些資源,這不就是 SOA 所提倡的“面向服務”的思想嗎?所以,REST 也被人們看做是一種輕量級的 SOA 實作技術,因此在企業級應用與互聯網應用中都得到了廣泛使用,
在 Java 的世界里,有一個名為 JAX-RS 的規范,它就是用來實作 REST 服務的,目前有許多框架已經實作了該規范,比如restlet、cxf,
cxf可以單獨使用,也可以與springframework繼承一起使用,下面講解第二種,
使用 Spring + CXF 發布 REST 服務
添加maven依賴
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
這里僅依賴 Spring Web 模塊(無需 MVC 模塊),此外就是 CXF 與 Jackson 了,
配置web.xml
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
使用 Spring 提供的 ContextLoaderListener 去加載 Spring 組態檔 spring.xml;使用 CXF 提供的 CXFServlet 去處理前綴為 /ws/ 的 REST 請求,
定義Resource資源類
public interface IBackupVaultRestService
{
@GET
@Path("/{siteId}/backup_vaults")
@Produces("application/json")
@Consumes("application/json")
String getBackupVaults(@PathParam("siteId") String siteId,
@QueryParam("limit") int limit,
@QueryParam("resourceType") String resourceType,
@QueryParam("protectType") String protectType,
@QueryParam("cloudType") String cloudType,
@QueryParam("startPage") int startPage);
}
public class BackupVaultRestServiceImpl extends AbstractRestService implements
IBackupVaultRestService
{......}
配置spring檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 發布配置添加的檔案 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />-->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 安全訪問認證攔截器 -->
<bean id="securityInterceptor"
class="com.tunsuy.rest.interceptor.RestSecurityInterceptor" />
<bean id="backupVaultRestService"
class="com.tunsuy.rest.service.platform.cloudresource.BackupVaultRestServiceImpl" />
<jaxrs:server id="siteServiceContainer" address="/sites">
<jaxrs:inInterceptors>
<ref bean="securityInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
</jaxrs:providers>
<jaxrs:serviceBeans>
<ref bean="backupVaultRestService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
這里加載了另一個組態檔 cxf.xml,其中包括了關于 CXF 的相關配置,另外,這里我們可以看到的標簽配置:這是使用了 CXF 提供的 Spring 命名空間來配置 Service Bean(即上文提到的 Resource Class)與 Provider,注意,這里配置了一個 address 屬性為“/sites”,表示 REST 請求的相對路徑,與 web.xml 中配置的“/ws/*”結合起來,最終的 REST 請求根路徑是“/ws/sites”,在 IBackupVaultRestService 介面方法上 @Path 注解所配置的路徑只是一個相對路徑,
將介面的實作類發布為SpringBean
有兩種方式:一是使用spring組態檔;一是使用注解,這里采用第一種,如上面spring檔案所示:
<bean id="backupVaultRestService"
class="com.huawei.ism.drm.rest.service.platform.cloudresource.BackupVaultRestServiceImpl" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/223361.html
標籤:其他
