Start
Refer to another article in this series:
Apache CXF implements Web Service (2)-A pure Jax-rs (RESTful) Web service without the use of heavyweight web containers and spring
First create a new dynamic web Project in Eclipse and then implement all the classes in the previous article, except here we don't need a server.java to start a Web Service. We use CXF's own org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet to launch a Web service service in the container.
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>CX F-test-servlet-rs-helloworld</artifactid> <packaging>war</packaging> <VERSION>1.0-SNAPSH ot</version> <name>cxf-test-servlet-rs-helloworld Maven webapp</name> <url>http://maven. Apache.org</url> <properties> <cxf.version>3.1.4</cxf.version> & lt;/properties> <dependencies> <dependency> <groupid>org.apache.cxf& Lt;/groupid> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf. Version}</version></dependency> <dependency> <groupId>org.apache.cxf</groupId> &L T;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> <artifa ctid>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> </d ependency> <dependency> <groupId>org.slf4j</groupId> <artifacti D>slf4j-api</artifactid> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-simple</a rtifactid> <version>1.5.8</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactid>commons-httpclient</artifa Ctid> ≪version>3.0</version> </dependency> <dependency> <groupid>comm Ons-io</groupid> <artifactId>commons-io</artifactId> <version>2.3</ve rsion> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scop e>test</scope> </dependency> </dependencies> <build> <fina lname>cxf</finalname> <resources> <resource> <directory> src/main/resources</directory> </resource> <resource> <dir Ectory>src/main/java</directory> <includes> <include>**</inc Lude> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactid>ma Ven-jetty-plugin</artifactid> <configuration> <contextpath>/</c ontextpath> <connectors> <connector implementation= "Org.mortbay. Jetty.nio.SelectChannelConnector "> <port>9000</port> </connector> </connectors> </configuration> </plug In> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>mavEn-compiler-plugin</artifactid> <configuration> <source>1.7</so urce> <target>1.7</target> </configuration> </p lugin> </plugins> </build> </project>
The main configuration is in 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>
You can see that we used Org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet this servlet includes using some of the startup parameters inside:
Jaxrs.serviceclasses
Jaxrs.providers
To configure a restful Web Service whose parameters are consistent with the parameters set in the code Jaxrsserverfactorybean in our previous article.
Start test
We run the Web project in Tomcat in Eclipse
Tomcat runs run as ... Run on Server
At last
We use the browser to access the Tomcat default 8080 port address: http://localhost:8080/cxf/services/roomservice/room
Supplemental servlet configuration parameter Templates
(Note: This is just a template, you need to modify the relevant parameters, do not copy and paste directly into the project)
<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> </i nit-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 J Son=application/json </param-value> </init-param><!--Registers contextual properties --<init-param> <param-name>jaxrs.properties</param-name> <param-value> Property1=value prope Rty2=value </param-value> </init-param><load-on-startup>1</load-on-startup></servlet >
Reference:
http://zzxanadu.iteye.com/blog/542249 (This still belongs to an example of a programatic implementation, except that the servlet load method was not validated successfully)
https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Services+Configuration# Jaxrsservicesconfiguration-withcxfnonspringjaxrsservlet
Apache CXF implements Web Service (3)--tomcat container and normal Servletjax-rs (RESTful) Web service without spring