Apache CXF實現Web Service(3)——Tomcat容器和不借助Spring的普通Servlet實現JAX-RS(RESTful) web service

Richaaaard發表於2015-12-02

起步

參照這一系列的另外一篇文章:

Apache CXF實現Web Service(2)——不借助重量級Web容器和Spring實現一個純的JAX-RS(RESTful) web service

首先在eclipse中新建一個Dynamic Web Project,然後實現上篇文章中的所有類,唯一不同的是這裡,我們不需要一個Server.java來啟動一個Web Service。我們用CXF自帶的org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet來在容器中啟動一個Web Service服務。

pom.xml

<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.cnblog.richaaaard.cxftest</groupId>  
    <artifactId>cxf-test-servlet-rs-helloworld</artifactId>  
    <packaging>war</packaging>  
    <version>1.0-SNAPSHOT</version>  
    <name>cxf-test-servlet-rs-helloworld Maven Webapp</name>  
    <url>http://maven.apache.org</url>  
      
    <properties>  
        <cxf.version>3.1.4</cxf.version>           
    </properties>  
      
    <dependencies>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http-jetty</artifactId>  
            <version>${cxf.version}</version>  
        </dependency> 
         <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>${cxf.version}</version>
    </dependency>
	<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>1.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.0</version>
    </dependency>
        <dependency>  
            <groupId>javax.ws.rs</groupId>  
            <artifactId>jsr311-api</artifactId>  
            <version>1.1.1</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>1.5.8</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-simple</artifactId>  
            <version>1.5.8</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-httpclient</groupId>  
            <artifactId>commons-httpclient</artifactId>  
            <version>3.0</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>2.3</version>  
        </dependency>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.8.1</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>  
      
    <build>  
        <finalName>cxf</finalName>  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
            </resource>  
            <resource>  
                <directory>src/main/java</directory>  
                <includes>  
                    <include>**</include>  
                </includes>  
                <excludes>  
                    <exclude>**/*.java</exclude>  
                </excludes>  
            </resource>  
        </resources>  
        <plugins>  
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>maven-jetty-plugin</artifactId>  
                <configuration>  
                    <contextPath>/</contextPath>  
                    <connectors>  
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                            <port>9000</port>  
                        </connector>  
                    </connectors>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.7</source>  
                    <target>1.7</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>  

  主要的配置都在web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>cxf</display-name>  
   
  <servlet>
 <servlet-name>cxf</servlet-name>
 <display-name>cxf</display-name>
 <servlet-class>
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 </servlet-class>
 <init-param>
  <param-name>jaxrs.serviceClasses</param-name>
  <param-value>
   	 com.cnblog.richaaaard.cxftest.servlet.rs.helloworld.service.RoomService      
  </param-value>
 </init-param>
 <init-param>
  <param-name>jaxrs.providers</param-name>
  <param-value>
    org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider     
  </param-value>
 </init-param>

<load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
</web-app>

  可以發現我們使用了org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet這個Servlet包括使用裡面一些啟動引數:

jaxrs.serviceClasses
jaxrs.providers

來配置RESTful Web Service,其引數與我們上篇文章中用程式碼JAXRSServerFactoryBean的引數設定一致。

啟動測試

我們在Eclipse中,將Web專案在Tomcat中執行

Tomcat執行Run As... -> Run on Server

 

最後

我們用瀏覽器訪問Tomcat預設的8080埠地址:http://localhost:8080/cxf/services/roomservice/room

補充 Servlet配置引數模板

(注意:這只是一個模板,需要修改相關引數,請勿直接複製貼上到專案中)

<servlet>
 <servlet-name>CXFServlet</servlet-name>
 <display-name>CXF Servlet</display-name>
 <servlet-class>
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 </servlet-class>
 <init-param>
  <param-name>jaxrs.serviceClasses</param-name>
  <param-value>
    org.apache.cxf.systest.jaxrs.BookStore1,
    org.apache.cxf.systest.jaxrs.BookStore2          
  </param-value>
 </init-param>
 <init-param>
  <param-name>jaxrs.providers</param-name>
  <param-value>
    org.apache.cxf.systest.jaxrs.BookStoreProvider1,
    org.apache.cxf.systest.jaxrs.BookStoreProvider2          
  </param-value>
 </init-param>
 <!-- enables schema validation -->
 <init-param>
  <param-name>jaxrs.schemaLocations</param-name>
  <param-value>
    classpath:/WEB-INF/schemas/schema1.xsd
    classpath:/WEB-INF/schemas/schema2.xsd           
  </param-value>
 </init-param>
 <!-- registers CXF in interceptors -->
 <init-param>
  <param-name>jaxrs.inInterceptors</param-name>
  <param-value>
    org.apache.cxf.systest.jaxrs.CustomInInterceptor
  </param-value>
 </init-param>
 <!-- registers CXF out interceptors -->
 <init-param>
  <param-name>jaxrs.outInterceptors</param-name>
  <param-value>
    org.apache.cxf.systest.jaxrs.CustomOutInterceptor
  </param-value>
 </init-param>
 <!-- registers extension mappings -->
 <init-param>
  <param-name>jaxrs.extensions</param-name>
  <param-value>
    xml=application/xml
    json=application/json
  </param-value>
 </init-param>
 <!-- registers contextual properties -->
 <init-param>
  <param-name>jaxrs.properties</param-name>
  <param-value>
    property1=value
    property2=value
  </param-value>
 </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

  

 

參考:

http://zzxanadu.iteye.com/blog/542249(這仍然屬於一個programatic實現的例子,只不過做到了servlet load方法中,未驗證成功)

https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Services+Configuration#JAXRSServicesConfiguration-WithCXFNonSpringJaxrsServlet

 

相關文章