基于CXF,Spring发布SOAP服务
1.新建一个Dynamic Web project(工程名CxfSpringWs),复制所需的jar包(apache-cxf-3.1.4/lib下所有jar)到WEB-INF/lib下
2.新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务
package com.mark.server; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { public String sayHello(@WebParam(name="arg0")String text); }
3.再新建该接口的实现类HelloWorldImpl.java
package com.mark.server; import javax.jws.WebService; @WebService(endpointInterface= "com.mark.server.IHelloWorld") public class HelloWorldImpl implements IHelloWorld { public String sayHello(String text) { return "Hello " + text; } }
4.现在可以进行spring配置了,在src文件夹下新建applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-servlet.xml" /> <bean id="hello"class="com.mark.server.HelloWorldImpl"/> <jaxws:endpointid="helloWorld" implementor="#hello"address="/HelloWorld" /> </beans>
5.配置web.xml
<?xmlversion="1.0" encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"version="3.0"> <display-name>CxfSpringWs</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>/*</url-pattern> </servlet-mapping> </web-app>
6.将应用部署到tomcat,启动并访问http://localhost:8080/CxfSpringWs/HelloWorld?wsdl,如果能正确显示xml文件则说明部署成功
7.调用SOAP服务,生成客户端代码
wsdl2java -p com.mark.client -encoding utf-8 -d d:\demo http://localhost:8080/CxfSpringWs/HelloWorld?wsdl
8.将代码复制到src下,编写调用逻辑
package com.mark.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class WsClient { public static void main(String[] args) { JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setAddress("http://localhost:8080/CxfSpringWs/HelloWorld"); factoryBean.setServiceClass(IHelloWorld.class); IHelloWorld service = (IHelloWorld) factoryBean.create(); System.out.println(service.sayHello("cxf")); } }