我这里使用的是最新的版本cxf3.1.8
引入cxf3需要在pom.xml加入如下内容:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.8</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.8</version> </dependency>
1、修改web.xml,增加如下内容不:
<!-- cxf webservices --> <servlet> <description>CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
2、添加spring的配置文件application-cxf.xml,内容如下(web.xml中扫描spring配置文件需要增加对这个配置文件的扫描):
<?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-servlet.xml" /> <!-- Cxf WebService 服务端示例 --> <jaxws:endpoint id="DemoWebservice" implementor="com.comp.web.webservice.impl.DemoWebserviceImpl" address="/demo"/> <!-- Cxf WebService 客户端示例 --> <jaxws:client id="demoClient" serviceClass="com.comp.web.webservice.DemoWebservice" address="http://localhost:8080/App/services/demo?wsdl" /> </beans>
注意看上面有服务端以及客户端的示例声明,下面会给出编写服务端及客户端的具体代码
1、application-cxf.xml文件中添加声明:
<!-- Cxf WebService 服务端示例 --> <jaxws:endpoint id="DemoWebservice" implementor="com.comp.web.webservice.impl.DemoWebserviceImpl" address="/demo"/>
2、接口文件DemoWebservice.java,代码如下:
package com.huarui.web.webservice; import javax.jws.WebService; @WebService public interface DemoWebservice { public String queryBaseTx(); public String queryBaseTxById(String id); }
3、实现文件DemoWebserviceImpl.java,代码如下:
package com.comp.web.webservice.impl; import java.util.List; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import com.google.gson.Gson; import com.comp.mapping.entity.BasetxEntity; import com.comp.service.jdbc.BasetxService; import com.comp.web.webservice.DemoWebservice; @WebService(endpointInterface = "com.comp.web.webservice.DemoWebservice") public class DemoWebserviceImpl implements DemoWebservice { @Autowired private BasetxService basetxService; public String queryBaseTx() { List list = basetxService.findAll(); return new Gson().toJson(list); } public String queryBaseTxById(String id) { BasetxEntity basetx = basetxService.findById(id); return new Gson().toJson(basetx); } }
重启项目,用这个地址就可以访问我们创建的服务列表了 http://localhost:8080/App/services/
对于我们刚才创建的服务的访问地址为:http://localhost:8080/App/services/demo?wsdl
路径中services对应的是web.xml文件中配置的内容
demo?wsdl对应的是application-cxf.xml文件中的address属性
1、application-cxf.xml文件中添加声明:
<!-- Cxf WebService 客户端示例 --> <jaxws:client id="demoClient" serviceClass="com.comp.web.webservice.DemoWebservice" address="http://localhost:8080/App/services/demo?wsdl" />
2、controller类中,添加如下代码:
@Autowired @Qualifier("demoClient") private DemoWebservice demo; ...... String result = demo.queryBaseTx(); System.out.println(result);
com.comp.web.webservice.DemoWebservice是基于服务端的接口实现,本来是要重新写的,我这里偷了个懒,直接用了服务端的代码
Qualifier注解里面的demoClient对应的是application-cxf.xml配置里面jaxws:client标签的id属性
com.comp.web.webservice.DemoWebservice是接口,没有实现类,通过jaxws:client标签调用服务地址生成实现类
这种方式不需要配置application-cxf.xml文件
在controller类中,添加如下代码:
URL wsdlURL = new URL("http://localhost:8080/App/services/demo?wsdl"); QName SERVICE_NAME = new QName("http://impl.webservice.web.comp.com/", "DemoWebserviceImplService"); Service service = Service.create(wsdlURL, SERVICE_NAME); DemoWebservice client = service.getPort(DemoWebservice.class); String result = client.queryBaseTx(); System.out.println(result);
我建议使用第一种方式,第二种方式这个QName需要从服务端接口里去找,然后我想说的是,QName并不太好找,写错了代码执行会报错。
我这里把我生成的服务端wsdl的内容贴出来:
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservice.web.huarui.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://webservice.web.huarui.com/"
name="DemoWebserviceImplService" targetNamespace="http://impl.webservice.web.comp.com/"> <wsdl:import location="http://localhost:8080/App/services/demo?wsdl=DemoWebservice.wsdl" namespace="http://webservice.web.comp.com/"></wsdl:import> <wsdl:binding name="DemoWebserviceImplServiceSoapBinding" type="ns1:DemoWebservice"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="queryBaseTx"> <soap:operation soapAction="" style="document"/> <wsdl:input name="queryBaseTx"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="queryBaseTxResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="queryBaseTxById"> <soap:operation soapAction="" style="document"/> <wsdl:input name="queryBaseTxById"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="queryBaseTxByIdResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="DemoWebserviceImplService"> <wsdl:port binding="tns:DemoWebserviceImplServiceSoapBinding" name="DemoWebserviceImplPort"> <soap:address location="http://localhost:8080/App/services/demo"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
注意看一下,这个QName里面的参数对应的是wsdl:definitions标签里面的targetNamespace属性和name属性
使用cxf工具里面的wsdl2java生成客户端代码,我参考的这篇文章:http://blog.csdn.net/hu_shengyang/article/details/38384839
我这里不再介绍这种方法
我生成客户端代码的命令是:
wsdl2java -p com.comp.web.client -d d:\web\client -client http://localhost:8080/App/services/demo?wsdl