1. Create HelloWorld Interface class
Package Com.googlecode.garbagecan.cxfstudy.helloworld; Import Javax.jws.WebParam; Import Javax.jws.WebResult; Import Javax.jws.WebService; @WebService public interface HelloWorld {public @WebResult (name= "string") string Sayhi (@WebParam (name= "text") string Text); }
2. Create HelloWorld Implementation class
Package Com.googlecode.garbagecan.cxfstudy.helloworld; public class Helloworldimpl implements HelloWorld {public string Sayhi (string name) {string msg = "Hello" + name + "!" ; System.out.println ("Server:" + msg); return msg; } }
3. Modify the Web.xml file
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "> <web-app> <display-name>cxfstudy</display-name> <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>/ws/*</url-pattern> </servlet-mapping> <listener> < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener > <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:* */spring.xml</param-value> </context-param> </web-app>
4. Create the spring configuration file and place it under the Classpath path
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 "> <import resource=" classpath:meta-inf/cxf/ Cxf.xml "/> <import resource=" classpath:meta-inf/cxf/cxf-extension-soap.xml "/> <import" Classpath:meta-inf/cxf/cxf-servlet.xml "/> <jaxws:endpoint id=" HelloWorld "implementor=" Com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl "address="/helloworld/> <!--for client test-- > <jaxws:client id= "helloworldclient" address= "Http://localhost:9000/ws/HelloWorld" Com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld "/> </beans>
5. Create a test class
Package Com.googlecode.garbagecan.cxfstudy.helloworld; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Springclient {public static void main (string[] args) {ApplicationContext context = new Classpathxmlapplicat Ioncontext ("Spring.xml"); HelloWorld HelloWorld = (HelloWorld) context.getbean ("Helloworldclient"); System.out.println (Helloworld.sayhi ("kongxx")); } }
6. Test
6.1. Start Tomcat first or use Maven's jetty and visit http://localhost:9000/ws/HelloWorld?wsdl to verify that the Web service has been started and is in effect;
6.2. Then run the test class to validate the Web service.