通过wsimport生成本地代理来访问自己发布的webservice

工具: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());
	}
}

输出结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值