spring webservice (二) 客户端开发

spring webservice (二) 客户端开发

上一篇文章  spring webservice (一) 服务器端开发  实现spring webservice服务端的开发与部署。
本部分将完成client的开发,我们将客户端与spring集成在一起。

1. 根据wsdl文件生成相应的java文件

请参考我的spring webservice(一)里面介绍的用wsimport生成java文件,按照里面的2.4节完成修改,这里我们只需要
User.java和IUserService.java两个文件,请注意对于IUserService.java文件再做一下修改:
@WebService(name = "IUserService", targetNamespace = "http://webservice.zdsoft.com/namespace/userservice")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface IUserService {

去掉上面的@XmlSeeAlso({jectFactory.class})

2. 配置spring-beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.zdsoft.webservice"/>
	
	<!-- 这样就可以在客户端把一个WebService注入到其它的bean中了 -->
	<bean id="userService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
		<property name="serviceInterface" value="com.zdsoft.webservice.api.user.IUserService"/>
		<property name="wsdlDocumentUrl" value="http://localhost:8080/spring-webservice-server/service/UserService.wsdl"/>
		<property name="namespaceUri" value="http://webservice.zdsoft.com/namespace/userservice"/>
		<property name="serviceName" value="UserService"/>
		<property name="portName" value="userServicePort"/>
		<!-- 使用handlerResolver属性来启用Handler,不过该属性要求其值为javax.xml.ws.handler.HandlerResolver类型 -->
		<!-- 所以需自定义一个类来实现HandlerResolver接口,具体写法见com.jadyer.handler.HeaderHandlerResolver.java -->
		<!-- <property name="handlerResolver" ref="headerHandlerResolver"/> -->
		<property name="customProperties">
			<map>
				<entry key="com.sun.xml.ws.request.timeout" value="20000" />
				<entry key="com.sun.xml.ws.connect.timeout" value="20000" />
			</map>
		</property>
	</bean>	
</beans>
PS:上面的属性namespaceUri,serviceName,portName请参考wsdl对应的属性做配置

3. 编写MainClient.java文件,如下:

package com.zdsoft.webservice.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zdsoft.webservice.api.user.IUserService;
import com.zdsoft.webservice.api.user.User;


/**
 * @author YuYang(zdsoft.yang@foxmail.com)
 *
 * @date 2014年5月2日
 */
public class MainClient {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");
		IUserService userService = (IUserService)ctx.getBean("userService");
		
		String loginInfo = userService.login("scott", "tiger");
		System.out.println(loginInfo);
		
		User user = userService.getUser("scott");
		
		System.out.println("---------User---------");
		System.out.println("username:" + user.getUsername());
		System.out.println("nickname:" + user.getNickname());
		System.out.println("password:" + user.getPassword());
	}
}

4. 运行main方法将看到下面的信息:

login success.
---------User---------
username:scott--username
nickname:scott--nickname
password:scott--password

spring webservice客户端的编写到此结束。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值