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

起步

参照这一系列的另外一篇文章:

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中运行

Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service_第1张图片

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

Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service_第2张图片

 

Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service_第3张图片

最后

我们用浏览器访问Tomcat默认的8080端口地址:http://localhost:8080/cxf/services/roomservice/room

Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service_第4张图片

补充 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

 

 

你可能感兴趣的:(Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service)