天天看點

WebService_CXF_RS

CXF-RS(Restful)風格:所有資源都共享統一的接口,以便在用戶端和伺服器之間傳輸狀态,使用的是标準的 HTTP 方法,比如 GET、PUT、POST 和 DELETE,可以支援多種消息格式-->xml,json.更易于實作緩存機制,第一次通路資源緩存,第二次通路資源傳回304查找本地資源.

對于請求方式的不同所對應的操作不同:

POST:儲存操作

PUT:修改操作

GET:查詢操作

DELETE:删除操作

對于同一請求方式,同一請求路徑,可以通過不同的參數進行不同的操作或傳回不同的資源。

一、WebService-CXF的rs風格獨立開發:

開發前提-->導入依賴:

<dependencies>
    <!-- 使用CXF RS開發 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 内置jetty web伺服器 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 使用log4j日志實作  -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>		
    <!-- 使用rs用戶端  -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 在CXF擴充提供者,提供轉換json接口  -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- cxf 擴充提供者 轉換json 預設需求一個工具包  -->
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.7</version>
    </dependency>
</dependencies>
           

1、對需要傳遞的資料對象實體類上要添加@XmlRootElement(name="對象名")注解,用于轉換為xml或json格式進行資料傳輸.

2、對釋出的業務接口上有以下注解:

       @Path("/接口服務的通路路徑")

3、對釋出的業務接口的方法上有以下注解:

     ①、@GET 查詢 @PUT 修改 @POST 增加 @DELETE 删除

     ②、@Path("/通路該方法的路徑/{參數}") 服務通路資源路徑

     ③、@Consumes({ "application/xml", "application/json" }) //consumes消費,方法參數

            @Produces({ "application/xml", "application/json" }) //produces生産,方法傳回值

4、對釋出的業務接口的方法參數上有以下注解:

    ①、(@PathParam("id") Integer id) -->PathParam的名稱應與@Path通路路徑後的參數名一緻

    ②、(@QueryParam("username") String username,@QueryParam("password") String password) -->QueryParam類似于http的get請求參數,一般用于傳遞多個參數,由?&&...拼接組成.

5、釋出服務:

public class RS_Server {
    public static void main(String[] args) {
        //建立業務接口實作類對象
        IUserService userService = new UserServiceImpl();
        //建立服務
        JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
        //指定将哪些實體類裝換成json或xml
        jaxrsServerFactoryBean.setResourceClasses(Car.class,User.class);
        jaxrsServerFactoryBean.setAddress("http://localhost:8888");
        jaxrsServerFactoryBean.setServiceBean(userService);		
        //釋出項目
        jaxrsServerFactoryBean.create();
    }
}
           

6、建立用戶端通路資源:

public class RS_Client {
    public static void main(String[] args) {
        /*
         * create 建立與服務資源調用的連接配接
         * type 發送給伺服器的資料格式 -- @Consumes
         * accept 接收伺服器傳輸的資料格式 -- @Produces
         * get/put/post/delete
         */
        Collection<? extends User> collection = WebClient.create("http://localhost:8889/userService/user")
                .accept(MediaType.APPLICATION_JSON).getCollection(User.class);
        System.out.println(collection);
        /*User user = new User();
        WebClient.create("http://localhost:8889/userService/user").type(MediaType.APPLICATION_XML).post(user);*/
    }
}
           

二、WebService-CXF的Spring整合的rs風格獨立開發:

開發前提-->①導入依賴:導入日志的配置檔案

<dependencies>
    <!-- cxf 進行rs開發 必須導入  -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 日志引入  -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <!-- 用戶端 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 擴充json提供者 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- 轉換json工具包,被extension providers 依賴 -->
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.7</version>
    </dependency>
    <!-- spring 核心 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- spring web內建 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- spring 整合junit  -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- junit 開發包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
           

②.在web.xml中配置web開發的spring核心監聽器:

<!-- spring配置檔案位置 -->  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext.xml</param-value>  
</context-param>  
<!-- spring核心監聽器 -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener> 
           

③.在web.xml中配置cxf基于web通路的servlet配置:

<servlet>  
    <servlet-name>CXFService</servlet-name>  
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>CXFService</servlet-name>  
    <url-pattern>/services/*</url-pattern>  
</servlet-mapping> 
           

1、在伺服器端釋出服務:

<?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:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
	<!-- 
		address 釋出服務位址 
		servicesBeans 服務實作類 
	 -->
	<jaxrs:server id="service" address="/資源通路路徑" >
		<jaxrs:serviceBeans>
			<bean class="xx.xx.xx.服務接口實作類" /> 
		</jaxrs:serviceBeans>
		<jaxrs:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
		</jaxrs:inInterceptors>
		<jaxrs:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
		</jaxrs:outInterceptors>
	</jaxrs:server>
</beans>
           

2、在用戶端即可通路,同上...

ws與rs的差別:

ws:使用jdk的原始動态代理開發,基于方法,"動詞",資源傳輸基于soap協定-->xml+http

rs:限制條件遵守restful的原則,基于資源,"名詞",資源傳輸基于http協定,可以傳輸json和xml格式的資料,現階段比較主流

繼續閱讀