CXF与Spring做了集成,所以可以充分使用上Spring优点,让开发使用WebService是如些的美妙.怎么写WebSerivce接口,实现类在这不做重复,重点说下如何发布,及使用.
? 服务端的发布:
beans.xml
<?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"> <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" /> <!-- UserWs --> <jaxws:endpoint id="userWs" implementor="com.xxx.ws.code.server.impl.UserWsImpl" address="/UserWs" /> <!-- ProductWs --> <bean id="productWsBean" class="com.xxx.ws.code.server.impl.ProductWsImpl" /> <jaxws:endpoint id="productWs" implementor="#productWsBean" address="/ProductWs" /> </beans>
?web.xml
<!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>WebService Site Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.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> </web-app>
?
发布后可查看到:
http://localhost:8080/UserWs?wsdl
http://localhost:8080/ProductWs?wsdl
发布成功!
?
客户端的使用:
cxf-webservice-client.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="userWsClient" serviceClass="com.xxx.ws.code.server.UserWs" address="${cxf.webservice.host}/UserWs" /> <jaxws:client id="productWsClient" serviceClass="com.xxx.ws.code.server.ProductWs" address="${cxf.webservice.host}/ProductWs" /> </beans>
?
ApplicationContext context = ...; // your Spring ApplicationContext
UserWs?userWs = (UserWs?) context.getBean("userWs");
?