使用CXF 开发SOAP 的webService接口客户端
发布时间:2020-12-16 23:14:08 所属栏目:安全 来源:网络整理
导读:上次我们讲到了 soap的webService的服务端,接下来我们将会继续写改实例的客户端, 这个实例是往服务端同步数据的接口服务,所以要和服务端的例子一样准备wsdl 和xsd ,以及在maven工程中引入cxf 生成wsdl 的插件。 然后生成对应的 request、 response、head
上次我们讲到了 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; } @Override public 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> /** * 客户端测试类 * @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; } @Before public void setup(){ syncUserClientImpl = (SyncUserClient) SpringTestHelper.get().getBeanByClass(SyncUserClientImpl.class); } /* *测试客户端 */ @Test public 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; } } 看到服务端已经打印出 插入的数据,说明接口同步成功! 好了一个完整的客户端服务已经开发完毕。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |