This document uses cxf 2.6.x as an example and uses jsr311.jar. The current cxf latest version is 3.x, depending on the JSR version is also different, and the spring configuration file does not need to configure: <import resource = "classpath: META-INF/cxf/cxf-extension-soap.xml"/>.
You need to keep the above details during version upgrade.
Now we start to use cxf2.6.x for some demos.
1. First, build a Maven project. The complete content of POM. XML is as follows:
<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><artifactId>abc-api</artifactId><packaging>war</packaging><version>${global.version}</version><parent><groupId>com.abc.module</groupId><artifactId>abc-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><dependency><groupId>javax.ws.rs</groupId><artifactId>jsr311-api</artifactId><version>1.1.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.codehaus.jettison</groupId><artifactId>jettison</artifactId><version>1.3.5</version></dependency><dependency><groupId>axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>org.codehaus.woodstox</groupId><artifactId>stax2-api</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.jbarcode</groupId><artifactId>jbarcode</artifactId><version>0.2.8</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName></build></project>
2. Configure 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>fsp-api</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:application.xml</param-value></context-param><!-- spring context listener --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- CXF --><servlet><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping></web-app>
3. Create a WebService External Service Interface
import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface ILogisticsWsApi { /** * @param xmlParam * @return xml string */ @WebMethod(operationName = "itemConfirm") public String itemConfirm(@WebParam String xmlParam);}
4. Implement WebService Interfaces
public class LogisticsWsApiImpl implements ILogisticsWsApi { private Logger log = LoggerFactory.getLogger(getClass()); @Override public String itemConfirm(String xmlParam) { // TODO Auto-generated method stub log.debug("itemConfirm xml param : " + xmlParam); LogisticsMessage logisticsMessage = LogisticsFactory.createItemConfirmRequest(); logisticsMessage.decode(xmlParam); // to do something ... return response; }}
5. Configure spring XML to provide services for WebService
<?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:jaxws="http://cxf.apache.org/jaxws" 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/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/><bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><jaxws:endpoint id="logisticsWsApiServiceContainer" implementor="com.abc.api.service.LogisticsWsApiImpl" address="/logisticsWsApi" > <jaxws:inInterceptors> <ref bean="loggingInInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="loggingOutInterceptor"/> </jaxws:outInterceptors></jaxws:endpoint></beans>
So far, the WebService server code is complete. Starting the Web server can provide external services.
Assume that your current Maven project is named ABC-API. Then, the actual address for accessing WebService is http: // ip: Port/ABC-API/services/logisticswsapi? WSDL
6. Compile a junittester interface to test the previous WebService interface.
Import static Org. JUnit. assert. *; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import Org. JUnit. after; import Org. JUnit. before; import Org. JUnit. test; import COM. ABC. warehouse. service. ilogisticswsapi; public class logisticswsapitester {private jaxwsproxyfactorybean wsfactory; private string address = "http: // localhost: 8080/ABC-API/services/logisticswsapi "; /***** @ throws Java. lang. exception */@ before public void setup () throws exception {wsfactory = new jaxwsproxyfactorybean (); wsfactory. setaddress (Address); wsfactory. setserviceclass (ilogisticswsapi. class);}/***** @ throws Java. lang. exception */@ after public void teardown () throws exception {wsfactory = NULL;}/*** Test Method for {@ link COM. ABC. API. service. logisticswsapiimpl # itemconfirm (Java. lang. string )}. * // @ test public void testitemconfirm () {// fail ("not yet implemented"); string xmlparam = "Hello jaxws, welcome"; ilogisticswsapi logisticswsapi = (ilogisticswsapi) wsfactory. create (); string recvmessage = logisticswsapi. itemconfirm (xmlparam); assertequals (recvmessage, null );}
Of course, we can also configure jaxwsproxyfactorybean using the spring class or directly configure jaxws: client in spring.
<?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:jaxws="http://cxf.apache.org/jaxws" 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/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:client id="logisticsWsApi" serviceClass="com.abc.warehouse.service.ILogisticsWsApi" address="${logisticsWsApiAddress}"/></beans>
<bean id="logisticsWsApi" class="com.abc.warehouse.service.ILogisticsWsApi" factory-bean="clientFactory" factory-method="create" /><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.abc.warehouse.service.ILogisticsWsApi" /> <property name="address" value="${logisticsWsApiAddress}" /> <!-- http://localhost:8080/abc-api/services/logisticsWsApi --> </bean>
At this point, we have completed the development of the WebService server and client.
Among them, the logging (logger, message) function of the loggingininterceptor class may encounter garbled characters when outputting logs. This Garbled text will not affect actual business operations.
If you want to correct the garbled characters here, You can reload the function to solve the problem of decoding Chinese characters. The modification method is simple. For details, see apache cxf jaxrs basic application.
Basic Application of Apache cxf jaxws