之前写过一个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类,即可看到运行结果。