Project Required jar Package: Http://files.cnblogs.com/files/walk-the-Line/cxf-spirng.zip
First write a demo interface
Package Import Javax.jws.WebService, @WebService public interface Demo { string Sayhi (string text);}
And then you need its implementation class.
TargetNamespace is the package path to the interface
Package Importimport Cn.cxf.demo.Demo; @WebService (endpointinterface = "Cn.cxf.demo.Demo", servicename= "Demoservice", targetnamespace= "http://demo.cxf.cn/")public classimplements Demo { @Override public String sayhi ( String text) { return "Hi!" + text; }}
Add Applicationcontext.xml in src directory
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"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 "> <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" /> <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" /> <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" /> <!--Test Interface - <Jaxws:endpointID= "Demoservice"implementor= "Cn.cxf.demo.impl.DemoImpl"Address= "/demoservice" /> </Beans>
then modify the Web. xml file to get it done ~ (add the following on Web. xml)
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:applicationContext.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>/ws/*</Url-pattern> </servlet-mapping>
Publish it and visit Http://localhost:8080/cxfdemo/ws to see that the interface has been published successfully.
It's much easier to call the interface.
If you simply call the interface, you only need 4 jar packages:
Cxf-2.4.3.jar
Neethi-3.0.1.jar
Wsdl4j-1.6.2.jar
Xmlschema-core-2.0.1.jar
Can be selected from the jar package above.
The calling code is as follows:
Packagecn.cxf.test;Importorg.apache.cxf.endpoint.Client;Importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; Public classCxfwstest { Public Static voidMain (string[] args)throwsException {jaxwsdynamicclientfactory Factory=jaxwsdynamicclientfactory.newinstance (); Client Client= Factory.createclient ("http://localhost:8080/cxfdemo/ws/DemoService?wsdl"); Object[] Objs= Client.invoke ("Sayhi", "Ford"); System.out.println (objs[0].tostring ()); }}
Cxf+spring Publishing WebService and calling