创建Web Service步骤
一、创建服务端工程
1、WebServerTest (web 工程)
File—New—Other——
选择Dynamic Web Project
配置Tomcat服务器
点击Browse选择Tomcat所在目录
点击Finish
点击Finish完成WebServerTest的创建
2.、创建服务的类和方法
在Java Resources:src上右键新建一个class
package com.test.service;
public class WSTest {
public String sayHello(String name){
return "Hello, "+name;
}
}
3、创建Web service
配置发布的服务
4、发布服务
点击servers,右键单击空白处,New—Server
下一步
点击Add再点击Finish
查看服务的目录
启动服务器,输入http://localhost:8080/WebServerTest/services
有如下方法说明发布成功
5、测试服务
调用服务方法
http://localhost:8080/WebServerTest/services/WSTest?method=sayHello&arg0=world!
WSTest 服务类
sayHello 服务方法
Arg0=world 方法参数
二、创建客户端工程
1、新建一个普通工程Java Project (WSClient)
2、利用wsdl文件生成服务接口类
选择工程WSClient右键新建一个Web Service Client
点击下一步
选中WSTest.wsdl点击OK最后点击finish生成如下文件
3、建测试类
package com.test.service;
import java.net.URL;
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
WSTestService service = new WSTestServiceLocator();
//调用localhost的服务
WSTest wstest0 = service.getWSTest();
System.out.println(wstest0.sayHello("world"));
//调用其他路径的服务
URL portAddress = new URL("http://localhost:8080/WebServerTest/services/WSTest");
WSTest wstest1 = service.getWSTest(portAddress);
System.out.println(wstest1.sayHello("world"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行测试类
三、不需要任何配置文件调用web service
上面的客户端测试类需要通过服务器生成的wsdl文件来生成服务接口,但在其他电脑上没有任何配置文件怎么调用上面发布的web service?
通过使用axis提供的api即可,方法如下
public static void testToMySelf() {
try {
// 可以通过这个网址直接查看到wsdl文件
String endpoint = "http://10.10.114.163:8080/WebServerTest/services/WSTest?wsdl";
Service service = new Service();
// 通过service创建call对象
Call call = (Call) service.createCall();
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// "http://service.test.com/"是通过上面网址查看到wsdl文件的targetNamespace的值,"sayHello"为方法名
call.setOperationName(new QName("http://service.test.com/","sayHello"));
// 传入的参数及类型
call.addParameter(new QName("http://service.test.com/", "name"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
// 返回参数的类型call.setSOAPActionURI("http://service.test.com/sayHello");
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING);
// Object 数组封装了参数,
String ret = (String) call.invoke(new Object[] { "world!" });
System.out.println("--------" + ret);
} catch (Exception e) {
e.printStackTrace();
}
}
四、根据上面原理访问下天气预报的web服务
public static void WeatherTest() {
try {
String endpoint = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("http://WebXml.com.cn/",
"getWeatherbyCityName"));
call.addParameter(
new QName("http://WebXml.com.cn/", "theCityName"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR); // 返回参数的类型(不能用Array,否则报错)
call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");
Vector ret = (Vector) call.invoke(new Object[] { "大庆" });
System.out.println("--------" + ret);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
运行结果