定义服务接口以及实现
package com.cxf.service.iface; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface Helloworld { public String sayHello(@WebParam(name="text") String text); } package com.cxf.service.impl; import javax.jws.WebService; import com.cxf.service.iface.Helloworld; @WebService(serviceName="Helloworld") public class HelloworldImpl implements Helloworld { public String sayHello(String text) { return "hello " + text; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-cxf-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
定义 Web Service 服务
<?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" 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"> <!-- cxf 服务器端,用于创建并发布服务 --> <!-- 一下三个文件位于cxf-2.1.12.jar中 --> <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" /> <!-- 当web服务器启动的时候会创建并发布该服务 启动日志信息如下: 信息: Creating Service {http://impl.service.cxf.com/}Helloworld from class com.cxf.service.iface.Helloworld 2010-12-31 14:18:48 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be /myService --> <jaxws:endpoint id="Helloworld" implementor="com.cxf.service.impl.HelloworldImpl" address="/myService" /> </beans>
当启动Tomcat的时候 会加载 spring-cxf-server.xml 并且创建指定的service并发布, web service 服务的 url
由endpoint中指定的 address="/myService"
即http://localhost:8080/context_path/myService (tomcat6.x)
创建客户端:
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cxf-client.xml"); Helloworld helloworld = (Helloworld) ctx.getBean("helloworldClient"); String info = helloworld.sayHello("taoge11111"); System.out.println(info);
<?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" 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"> <!-- cxf 客户端 --> <jaxws:client id="helloworldClient" serviceClass="com.cxf.service.iface.Helloworld" address="http://localhost:8080/cxf/myService" /> </beans>