cxf3.* How to publish WebService services
How to build SPRING+CXF
First, in the Pom.xml add cxf3.1.14 dependence, JDK1.7
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.14</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.14</version>
</dependency>
Ii. configuring in the Web. XML and Spring***.xml files
<!--configuring Cxfwebservice listeners-- <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <load-on-startup>4</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/service/*</ url-pattern> </servlet-mapping>
CXF need to use Contextloaderlistener listener
(1) So I first introduced the Spring.xml core configuration file in Web. xml
1 <!--introducing Spring.xml files--2 <context-param>3 <param-name> Contextconfiglocation</param-name>4 <param-value>classpath:spring.xml</param-value >5 </context-param>6 <listener>7 <listener- Class>org.springframework.web.context.contextloaderlistener</listener-class>8 </listener>
(2) Introduce the CXF configuration file in the Spring.xml file Spring-cxf-service.xml
1 <!--introduce the XML configuration file in the CXF jar package--2 <import resource= "Spring-cxf-service.xml"/>
(3) Include the following header file in the Spring-cxf-service.xml configuration file
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM"4Xmlns:jaxws= "Http://cxf.apache.org/jaxws"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans-3.0.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM8http//www.springframework.org/schema/oxm/spring-oxm-3.0.xsd9http//Cxf.apache.org/jaxwsTenhttp//cxf.apache.org/schemas/jaxws.xsd ">
(4) Introducing an XML file with the CXF jar package
1 <!--cxf 3.* only need to introduce this one jar package with the XML file--2 <import resource= "Classpath: Meta-inf/cxf/cxf.xml "/>
To this basic configuration is complete.
Third, first create an interface, the class name is added @webservice annotations, the method plus @webmethod annotations
1 Package**.**.*****;2 3 ImportJavax.jws.WebMethod;4 ImportJavax.jws.WebParam;5 ImportJavax.jws.WebService;6 7 @WebService8 Public InterfaceImywebservice {9 @WebMethodTen PublicString FirstWebService (String xml)throwsException; One A}
To create an implementation class for an interface
1 PackageCn.ztesoft.webservice;2 3 ImportJavax.jws.WebService;4 Importorg.dom4j.Document;5 ImportOrg.dom4j.DocumentHelper;6 Importorg.dom4j.Element;7 8@WebService (endpointinterface= "Cn.ztesoft.webservice.IMyWebService")9 Public classMyServiceImplementsImywebservice {Ten One @Override A PublicString FirstWebService (String xml)throwsException { - System.out.println (XML); -Element root =NULL; theDocument document =documenthelper.parsetext (XML); -Root =document.getrootelement (); -String Interfacecode = Root.elementtext ("Posid"); - System.out.println (interfacecode); +StringBuffer str=NewStringBuffer (); - //Splicing Message +Str.append ("<status>1</status>"); AStr.append ("<errorMsg>22</errorMsg>"); at returnstr.tostring (); - } -}
Configuring in the Spring-cxf-service.xml core configuration file
class= "Cn.ztesoft.webservice.MyService" ></bean>2 <!--implementor Note that the parameters introduced are not preceded by #就报错, up to3 <!--ID is what this interface is called Implementor is its implementation class address is scoped to this interface service's addressing path--4 <jaxws:endpoint id= "myservicewsdl" address= "/myservice" implementor= "#myService"/>
After the last deployment to Tomcat, you can view the published WebService interface in the service service (configuration in Web. xml)
CXF 3.* WebService Server