利用webservice让一个程序访问到另一个程序的方法

webservice是什么,原理我就不多说,百度有很多解答,现在我分享的是我在写webservice一个小demo的总结:

首先,我们要创建一个程序来发布一个webservic,将方法暴露出来给另一个程序使用,在这里我是直接创建的接口:

@javax.jws.WebService
public interface WebService {

	@WebMethod
	public String sayHello(String name);
	
	@WebMethod
	public String sayGoodby(String name);
	
}




@javax.jws.WebService
public class WebServiceImpl implements WebService {

	@Override
	public String sayHello(String name) {
		String str = "hello " + name;
		return str;
	}

	@Override
	public String sayGoodby(String name) {
		String str = "goodby " + name;
		return str;
	}

}

值得一说的是在接口和实现类中,类名都要加上一个@webservice的注解(打一半选择提示就会出现上述代码的样子),在接口方法上要加上@webmethod注解,表示这个是要暴露出去的方法,但是这个注解里面有个参数exclude,默认值为false,如果为true则表示此方法不暴露出去。还有一种简单的写法:

@WebService
public class Function {

	// 该方法就是要暴露给其他应用程序调用的方法
	public String transWords(String words) {
		String res = "";
		for (char ch : words.toCharArray()) {
			res += "\t" + ch + "\t";
		}
		return res;
	}

	// 这里我们使用main方法来发布我们的service
	public static void  main(String[] args){
			Endpoint.publish("http://localhost:9001/Service/Function",new WebServiceImpl());
			System.out.println("Publish Success~");
	}

	
}

这个是我们的测试类,我们可以不要上面写的接口和实现类,直接用这个也是可行的,当我们将这三个文件都写出来时,test类里面的方法是不会暴露出去的,这个要注意,点击运行,控制台输出Publish Success~则表示运行成功。

然后在浏览器输入发布地址,后面不要忘记加上?wsdl,出现以下信息则表示发布成功:

然后在eclipse里面创建webservice client,新建一个项目,在自己创建的包上右键,选择new-》other-》webservice client-》输入浏览器里面的网址(带?wsdl),拉条选择deploy client,点击finish就会在相应包下生成文件,在以proxy结尾的.java文件下创建main方法,然后创建之前发布的实现类名称+Proxy的类的对象(应该就是当前的类),然后用创建的对象就可以调用方法,如下:

public class WebServiceImplProxy implements cn.http.demo.WebServiceImpl {
  private String _endpoint = null;
  private cn.http.demo.WebServiceImpl webServiceImpl = null;
  
  public WebServiceImplProxy() {
    _initWebServiceImplProxy();
  }
  
  public WebServiceImplProxy(String endpoint) {
    _endpoint = endpoint;
    _initWebServiceImplProxy();
  }
  
  private void _initWebServiceImplProxy() {
    try {
      webServiceImpl = (new cn.http.demo.WebServiceImplServiceLocator()).getWebServiceImplPort();
      if (webServiceImpl != null) {
        if (_endpoint != null)
          ((javax.xml.rpc.Stub)webServiceImpl)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
        else
          _endpoint = (String)((javax.xml.rpc.Stub)webServiceImpl)._getProperty("javax.xml.rpc.service.endpoint.address");
      }
      
    }
    catch (javax.xml.rpc.ServiceException serviceException) {}
  }
  
  public String getEndpoint() {
    return _endpoint;
  }
  
  public void setEndpoint(String endpoint) {
    _endpoint = endpoint;
    if (webServiceImpl != null)
      ((javax.xml.rpc.Stub)webServiceImpl)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
    
  }
  
  public cn.http.demo.WebServiceImpl getWebServiceImpl() {
    if (webServiceImpl == null)
      _initWebServiceImplProxy();
    return webServiceImpl;
  }
  
  public java.lang.String sayHello(java.lang.String arg0) throws java.rmi.RemoteException{
    if (webServiceImpl == null)
      _initWebServiceImplProxy();
    return webServiceImpl.sayHello(arg0);
  }
  
  public java.lang.String sayGoodby(java.lang.String arg0) throws java.rmi.RemoteException{
    if (webServiceImpl == null)
      _initWebServiceImplProxy();
    return webServiceImpl.sayGoodby(arg0);
  }
  




  //这个是自己创建的main方法,写完直接运行就可以看到运行结果
  public static void main(String[] args) throws RemoteException {
	  String endpoint = "http://localhost:9001/Service/Function";
	WebServiceImplProxy implProxy = new WebServiceImplProxy();
	String sayHello = implProxy.sayHello("小明");
	System.out.println(sayHello);
}
  
}

最后,运行程序,得到结果:hello 小明

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值