spring webservice (二) 客户端开发
2014-09-17 21:59
260 查看
上一篇文章 spring webservice (一) 服务器端开发 实现spring
webservice服务端的开发与部署。
本部分将完成client的开发,我们将客户端与spring集成在一起。
请参考我的spring webservice(一)里面介绍的用wsimport生成java文件,按照里面的2.4节完成修改,这里我们只需要
User.java和IUserService.java两个文件,请注意对于IUserService.java文件再做一下修改:
[java] view
plaincopy
<span style="color:#362e2b;"></span><pre code_snippet_id="323606" snippet_file_name="blog_20140502_1_2815689" name="code" class="java">@WebService(name = "IUserService", targetNamespace = "http://webservice.zdsoft.com/namespace/userservice")
@XmlSeeAlso({
ObjectFactory.class
})
public interface IUserService {</pre>
<pre></pre>
[html] view
plaincopy
<?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对应的属性做配置
plaincopy
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());
}
}
plaincopy
login success.
---------User---------
username:scott--username
nickname:scott--nickname
password:scott--password
spring webservice客户端的编写到此结束。
webservice服务端的开发与部署。
本部分将完成client的开发,我们将客户端与spring集成在一起。
1. 根据wsdl文件生成相应的java文件
请参考我的spring webservice(一)里面介绍的用wsimport生成java文件,按照里面的2.4节完成修改,这里我们只需要User.java和IUserService.java两个文件,请注意对于IUserService.java文件再做一下修改:
[java] view
plaincopy
<span style="color:#362e2b;"></span><pre code_snippet_id="323606" snippet_file_name="blog_20140502_1_2815689" name="code" class="java">@WebService(name = "IUserService", targetNamespace = "http://webservice.zdsoft.com/namespace/userservice")
@XmlSeeAlso({
ObjectFactory.class
})
public interface IUserService {</pre>
<pre></pre>
去掉上面的@XmlSeeAlso({jectFactory.class})
2. 配置spring-beans.xml文件,如下:
[html] viewplaincopy
<?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文件,如下:
[java] viewplaincopy
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方法将看到下面的信息:
[plain] viewplaincopy
login success.
---------User---------
username:scott--username
nickname:scott--nickname
password:scott--password
spring webservice客户端的编写到此结束。
相关文章推荐
- spring webservice (二) 客户端开发
- WebService Xfire+spring整合开发,哪位大哥给些资料。
- Eclipse安装CXF插件开发java web service 集成Spring
- CXF+WS-Security+Spring WebService服务器端+客户端及注意问题
- 在linux下,利用gSoap开发Web Service C/C++客户端
- 利用gSoap开发Web Service C/C++客户端
- 利用gSoap开发Web Service C/C++客户端
- 采用CXF框架开发HelloWorld WebService客户端
- EAS BOS Webservice的开发、部署及客户端使用
- spring webservice (一) 服务器端开发
- spring整合mina开发web项目和简单mina客户端的使用
- 使用gSoap开发Web Service C/C++客户端[转]
- webservice axis 集成spring,开发测试demo
- Eclipse安装CXF插件开发java web service 集成Spring
- 利用gSoap开发Web Service C/C++客户端
- XFire Web Service客户端开发
- Spring 4 集成Apache CXF开发JAX-RS Web Service
- Eclipse安装CXF插件开发java web service 集成Spring
- 在linux下,利用gSoap开发Web Service C/C++客户端
- 使用gSoap开发Web Service C/C++客户端