使用CXF 开发SOAP 的webService接口客户端

来源:互联网 发布:电线传输网络信号 编辑:程序博客网 时间:2024/05/16 16:16

上次我们讲到了 soap的webService的服务端,接下来我们将会继续写改实例的客户端,

这个实例是往服务端同步数据的接口服务,所以要和服务端的例子一样准备wsdl 和xsd ,以及在maven工程中引入cxf 生成wsdl 的插件。 然后生成对应的 request、

response、header等实体,以及生成接口的服务Service。

然后客户端要创建pojo实体,用来进行封装推送的信息。

UserEntity类:

/** * 用户实体 * @author leo * */public class UserEntity {//账号private String userCode;//姓名private String userName;//密码private String password;public String getUserCode() {return userCode;}public void setUserCode(String userCode) {this.userCode = userCode;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
接下来创建客户端的接口,以及实现类,

/** * 模拟webService客户端 * @author leo * */public interface SyncUserClient {void syncUser(UserEntity userEntity);}
实现类SyncUserClientImpl.java的代码

package com.deppon.soap.client.Service.impl;import java.util.UUID;import javax.xml.ws.Holder;import com.deppon.soap.bean.UserEntity;import com.deppon.soap.client.Service.SyncUserClient;import com.deppon.soap.header.ESBHeader;import com.deppon.soap.soapdemoservice.CommonException;import com.deppon.soap.soapdemoservice.DemoRequest;import com.deppon.soap.soapdemoservice.SoapDemoService;/** * <span style="font-family: Arial, Helvetica, sans-serif;">模拟客户端的实现</span> * @author leo * */public class SyncUserClientImpl implements SyncUserClient{//private SoapDemoService soapDemoService;public void setSoapDemoService(SoapDemoService soapDemoService) {this.soapDemoService = soapDemoService;}@Overridepublic void syncUser(UserEntity userEntity) {ESBHeader header = new ESBHeader();// 设置服务编码header.setEsbServiceCode("test");header.setMessageFormat("SOAP");header.setSourceSystem("soap test");header.setExchangePattern(1);header.setVersion("1.0");header.setRequestId(UUID.randomUUID().toString());Holder<ESBHeader> holder = new Holder<ESBHeader>(header);DemoRequest request = new DemoRequest();request.setUserCode(userEntity.getUserCode());request.setUserName(userEntity.getUserName());request.setTitle("haha");try {//调用接口soapDemoService.soapDemoService(holder, request);} catch (CommonException e) {e.printStackTrace();}}}
最后,由于Cxf 要依赖于spring的集成,且是webApp 服务,所以要在web.xml中配置CXF、spring等监听和过滤验证。然后在ds-spring.xml 中注入客户端的依赖,以及cxf主持spring集成的客户端配置:

<bean id="syncUserClientImpl" class="com.deppon.soap.client.Service.impl.SyncUserClientImpl"><property name="soapDemoService" ref="soapDemoService"></property></bean><!--  client --><jaxws:client id="soapDemoService" serviceClass="com.deppon.soap.soapdemoservice.SoapDemoService" address="http://127.0.0.1:8080/test/soap/userServiceImpl"><jaxws:inInterceptors><bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/></jaxws:inInterceptors><jaxws:outInterceptors><bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/></jaxws:outInterceptors></jaxws:client>


都配置好了之后,先启动服务端,然后写一个junit 测试类进行测试客户端,

/** * 客户端测试类 * @author leo * */public class TestClient {private static final Log LOG = LogFactory.getLog(TestClient.class);private SyncUserClient syncUserClientImpl;public void setSyncUserClientImpl(SyncUserClient syncUserClientImpl) {this.syncUserClientImpl = syncUserClientImpl;}@Beforepublic void setup(){syncUserClientImpl = (SyncUserClient) SpringTestHelper.get().getBeanByClass(SyncUserClientImpl.class);}/* *测试客户端  */@Testpublic void test() {UserEntity entity =createUser();syncUserClientImpl.syncUser(entity);}/** * 创建实体对象 * @return */public  UserEntity createUser(){UserEntity  entity = new UserEntity();entity.setUserName("老板");entity.setUserCode("000001");entity.setPassword("000000");return entity;}}


看到服务端已经打印出 插入的数据,说明接口同步成功! 好了一个完整的客户端服务已经开发完毕。

0 0
原创粉丝点击