工具:myeclipse
jdk版本:1.7(要1.6.21以上)
创建两个javaweb项目
WS_Server中的phone实体类:
package cn.itcast.model;
public class Phone {
private String pname;
private String powner;
private int ptotal;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPowner() {
return powner;
}
public void setPowner(String powner) {
this.powner = powner;
}
public int getPtotal() {
return ptotal;
}
public void setPtotal(int ptotal) {
this.ptotal = ptotal;
}
}
Server代码:
package cn.itcast.service.phone;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import cn.itcast.model.Phone;
/**
* 该业务类通过webservice对外提供服务
* @author Administrator
*
*/
@WebService(serviceName="PhoneManager", //修改服务名
targetNamespace="http://phone.service.itcast.cn")//修改命名空间,默认包名,取反
//声明该业务类 对外提供webservice服务,默认只是对public修饰的方法对外以webservice形式发布
public class PhoneService {
/**@WebMethod(operationName="getMObileInfo"): 修改方法名
* @WebResult(name="phone"):修改返回参数名
* @WebParam(name="osName"):修改输入参数名
*/
@WebMethod(operationName="getMObileInfo")
@WebResult(name="phone")
public Phone getPhoneInfo(@WebParam(name="osName")String osName){
Phone phone = new Phone();
if(osName.endsWith("android")){
phone.setPname("android");phone.setPowner("google");phone.setPtotal(80);
}else if(osName.endsWith("ios")){
phone.setPname("ios");phone.setPowner("apple");phone.setPtotal(15);
}else{
phone.setPname("windows phone");phone.setPowner("microsoft");phone.setPtotal(5);
}
return phone;
}
@WebMethod(exclude=true)//把该方法排除在外
public void sayHello(String city){
System.out.println("你好:"+city);
}
private void sayLuck(String city){
System.out.println("好久不见:"+city);
}
void sayGoodBye(String city){
System.out.println("拜拜:"+city);
}
protected void saySayalala(String city){
System.out.println("再见!"+city);
}
public static void main(String[] args){
String address1 = "http://127.0.0.1:8888/ws/phoneService";
String address2 = "http://127.0.0.1:8888/ws/phoneManager";
/**
* 发布webservice服务
* 1、address:服务地址
* 2、implementor:服务的实现对象
*/
Endpoint.publish(address1, new PhoneService());
Endpoint.publish(address2, new PhoneService());
System.out.println("wsdl地址 :"+address1+"?WSDL");
System.out.println("wsdl地址 :"+address2+"?WSDL");
}
}
发布成功后
在网页上输入http://127.0.0.1:8888/ws/phoneService?WSDL
或者http://127.0.0.1:8888/ws/phoneManager?WSDL
同时在这里补充一下,上面再Server的代码中还写了几个方法,只有public的才会在xml中显示出来,也只有public的方法才能被Client调用。
这里的sayHello方法已经被 @WebMethod(exclude=true)//把该方法排除在外,所以也不会显示
通过wsimport生成本地代理
- -s 表示要解析java的源码,后面写的是生成代理类的目录的位置 (-s ./ 表示在当前目录下生成)
- -p 后面写的是生成代理类储存的包的路径 (-p cn.itcast.phone 表示生成代理类储存cn.itcast.phone下)
如果只输入wsimport http://127.0.0.1:8888/ws/phoneService?WSDL
那就只会生成 .class 的文件,没有 .java 的文件
将生成的代理类放入Client项目中
Client代码:
package cn.itcast.phone;
public class Test {
public static void main(String[] args){
//服务
PhoneManager ws = new PhoneManager();
//访问方式
PhoneService phoneServicePort = ws.getPhoneServicePort();
//调用方法
Phone phoneInfo = phoneServicePort.getMObileInfo("ios");
System.out.println(phoneInfo.getPname());
System.out.println(phoneInfo.getPowner());
System.out.println(phoneInfo.getPtotal());
}
}
输出结果: