WebService的测试

本文介绍了如何通过WSDL文件和指定的服务地址来生成WebService客户端调用程序,包括通过WSDL文件自动生成客户端、根据服务地址直接创建客户端两种方式,并通过测试类验证了调用WebService的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。
[size=medium] [color=red]1)通过WSDL文件生成客户端调用程序[/color][/size]
首先我们通过http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我们可以获得WSDL文件HelloWorldService.wsdl,并将其放在src目录下面,接着我们通过程序访问该WSDL文件,并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示:

package test;

import org.codehaus.xfire.client.Client;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import webservice.HelloWorld;

/**
*Copyright2007GuangZhouAmigo.
*Allrightreserved.
*HelloWorld的webservice的测试类.
*@author<a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
*@version1.0
*Creationdate:2007-9-16-下午05:36:05
*/
public class WebServiceClientTest {
HelloWorld helloWorld = null;

public static void main(String[] args) throws Exception {
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}

public void testClient() throws Exception {
String wsdl = "HelloWorldService.wsdl"; //对应的WSDL文件
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null); //根据WSDL创建客户实例

Object[] objArray = new Object[1];
objArray[0] = "kk";
//调用特定的Web Service方法
Object[] results = client.invoke("sayHelloWorld", objArray);
System.out.println("result: " + results[0]);
}
}


运行该类,可得到如下输出结果:

result: hello,kk

可看出运行结果正确。

[size=medium][b]2)根据服务地址创建客户端调用程序[/b][/size]

接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立WebServiceClientTest.java文件,并在src目录下建立客户端调用的Spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebService的bean,该bean访问wsdlDocumentUrl为http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。该xml文件的详细内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass">
<value>webservice.HelloWorld</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl</value>
</property>
</bean>
</beans>


在WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示:


package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import webservice.HelloWorld;

/**
*HelloWorld的webservice的测试类.
*/
public class WebServiceClientTest {
HelloWorld helloWorld = null;

public static void main(String[] args) {
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}

public void testClient() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client.xml");
helloWorld = (HelloWorld) ctx.getBean("testWebService");
System.out.println(helloWorld.sayHelloWorld("kk"));
}
}


在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息:

hello,kk

由此可看出调用Web Service成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值