之前寫過一個CXF整合spring,那是的CXF是2.2的,Spring是3.2的,前些日子去CXF官網下了個3.1的,用Spring4.3的整合,發現用舊版本配置的方式來配置新版的已不管用了,這兩天又在網上搜了CXF3.0和Spring4的整合方式,原來使用的包都變了,這又重新整理一份,真麻煩。
接口
- package test;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface Hello {
- public String say(@WebParam(name="name")String name);
- }
實現
- package test;
- import javax.jws.WebService;
- @WebService(endpointInterface="test.Hello")
- public class HelloImp implements Hello {
- public String say(String name) {
- return "hello,"+name;
- }
- }
bean.xml(或applicationContext.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:context="http://www.springframework.org/schema/context"
- 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://www.springframework.org/schema/context
- http://www.springframework.org/schema/aop/spring-context.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <jaxws:endpoint id="abc" address="/cde" implementor="test.HelloImp"></jaxws:endpoint>
- </beans>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
- http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID"
- version="3.1">
- <display-name>test</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>
- <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>/service/*</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:bean.xml</param-value>
- </context-param>
- </web-app>
使用的包(缺一不可)
訪問URL:http://localhost:8080/test/service/cde?wsdl
生成客戶端命令:wsdl2java -client -p client -d e:/ http://localhost:8080/test/service/cde?wsdl
把生成的客戶端放入到項目裏,直接運行其中xxxxxxx_Client.Java類,即可看到運行結果。