您的位置:首页 > 编程语言 > Java开发

spring webservice (二) 客户端开发

2014-09-17 21:59 260 查看
上一篇文章 spring webservice (一) 服务器端开发 实现spring
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] 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对应的属性做配置

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

[java] view
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());

}

}

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

[plain] view
plaincopy





login success.

---------User---------

username:scott--username

nickname:scott--nickname

password:scott--password

spring webservice客户端的编写到此结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: