Introduction:Apache cxf is an open-source framework that provides a reliable infrastructure for easy construction and development of Web Services. It allows you to create high-performance and scalable services. You can deploy such services in Tomcat, spring-based lightweight containers, and more advanced servers, for example, JBoss, IBM WebSphere, or BEA WebLogic.
Start building:
1. Server Creation
Figure: Project Structure
1. Configure the maven file and add the cxf development kit [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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><artifactId>rojeff-labs</artifactId><groupId>com.rojeff.labs</groupId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.rojeff.labs</groupId><artifactId>cxf-labs-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>cxf-labs-server</name><properties><cxf.version>2.6.2</cxf.version><spring.version>3.1.2.RELEASE</spring.version><slf4j.version>1.7.1</slf4j.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</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.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency></dependencies></project>
2. Configure the Web. xml file [WEB-INF/Web. xml]
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-cxf.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping></web-app>
3. Configure the spring and cxf integration files [applicationcontext. xml]
<?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:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- Enable annotation configuration --><context:annotation-config /><!-- Scan system packages for Spring annotations --><context:component-scan base-package="com.rojeff.labs" /><jaxws:endpoint id="helloWorld"implementor="com.rojeff.labs.HelloWorldImpl" address="/HelloWorld" /></beans>
4. Interface [helloworld. Java]
package com.rojeff.labs;import javax.jws.WebService;@WebService public interface HelloWorld { public String sayHello(String text);}
5. Implementation class [helloworldimpl. Java]
package com.rojeff.labs;import javax.jws.WebService;@WebService(endpointInterface = "com.rojeff.labs.HelloWorld")public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String text) { return "Hello" + text; }}
I. Client Creation
Figure: Project Structure
1. Configure the maven file and add the cxf development kit [Pom. xml]
It must be the same as the Pom. xml file on the server.
2. Interface [helloworld. Java]
package com.rojeff.labs;import javax.jws.WebService;@WebService public interface HelloWorld { public String sayHello(String text);}
3. Configure the spring and cxf integration files [applicationcontext. xml]
<?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:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- Enable annotation configuration --><context:annotation-config /><!-- Scan system packages for Spring annotations --><context:component-scan base-package="com.rojeff.labs" /><bean id="helloWorld" class="com.rojeff.labs.HelloWorld" factory-bean="helloWorldFactory"factory-method="create" /><bean id="helloWorldFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.rojeff.labs.HelloWorld" /><property name="address" value="http://localhost:8080/cxf-labs-server/HelloWorld" /></bean></beans>
4. Test class [helloworldtest. Java]
package com.rojeff.labs;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(value = { "/META-INF/spring/applicationContext.xml" })public class HelloWorldTest { @Autowired HelloWorld helloWorld; @Test public void testSayHello() { System.out.println(helloWorld.sayHello("luo")); }}
Download example:Cxf sample code-spring MVC sample code-20130313