加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice_01_快速实例

发布时间:2020-12-17 00:12:42 所属栏目:安全 来源:网络整理
导读:非常感谢 孙浩 老师的视频和资料。 ? 接口: package cn.lichen.webservice;import javax.jws.WebService;@WebServicepublic interface IMyService {public int add(int a,int b);public int minus(int a,int b);} 实现: package cn.lichen.webservice;impo

非常感谢孙浩老师的视频和资料。

?

接口:

package cn.lichen.webservice;

import javax.jws.WebService;

@WebService
public interface IMyService {
	
	public int add(int a,int b);
	
	public int minus(int a,int b);
}


实现:

package cn.lichen.webservice;

import javax.jws.WebService;

@WebService(endpointInterface="cn.lichen.webservice.IMyService")
public class MyServiceImpl implements IMyService {

	@Override
	public int add(int a,int b) {
		System.out.println(a+"+"+b+"="+(a+b));
		return (a+b);
	}

	@Override
	public int minus(int a,int b) {
		System.out.println(a+"-"+b+"="+(a-b));
		return (a-b);
	}

}


“服务器”:

package cn.lichen.webservice;

import javax.xml.ws.Endpoint;

public class MyService {

	public static void main(String[] args) {
		String address = "http://localhost:8888/ns";
		Endpoint.publish(address,new MyServiceImpl());
	}
}


?

“客户端”:

package cn.lichen.webservice;

import java.net.MalformedURLException;

public class Client {
	public static void main(String[] args) {
		
		try {
			//创建访问wsdl服务地址的url
			URL url = new URL("http://localhost:8888/ns?wsdl");
			//通过Qname指明服务的具体信息
			QName sname = new QName("http://webservice.lichen.cn/","MyServiceImplService");
			//创建服务
			Service service = Service.create(url,sname);
			//实现接口
			IMyService ms = service.getPort(IMyService.class);
			System.out.println(ms.minus(88,66));
			//以上服务有问题,依然依赖于IMyServie接口
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}


?

先启动“服务器”,再启动“客户端”,就能得到结果。?

注意 接口添加@WebService 和 实现 添加 @WebService(endpointInterface="cn.lichen.webservice.IMyService")

?

浏览器打开指定的URL:http://localhost:8888/ns?wsdl

?

注意namespace和definitions name为Client的Qname的参数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读