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客户端的编写到此结束。