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

WebService学习笔记

发布时间:2020-12-16 23:07:58 所属栏目:安全 来源:网络整理
导读:首先创建一个简单的webService的小案例。 设计接口--设计接口的实现类--创建服务--测试类 下面依次是三个步骤的代码。 ①设计接口IMyservice import javax.jws.WebService;@WebServicepublic interface IMyService {public int add(int a,int b);public int

首先创建一个简单的webService的小案例。

设计接口-->设计接口的实现类-->创建服务-->测试类


下面依次是三个步骤的代码。

①设计接口IMyservice

import javax.jws.WebService;

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

	public int minus(int a,int b);
}

②设计接口实现类MyServiceImpl

import javax.jws.WebService;

@WebService(endpointInterface="earl.service.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;
	}

}

注:必须为接口和接口的实现类添加注解。


③创建服务MyServer

import javax.xml.ws.Endpoint;

public class MyServer {

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

}

④创建测试类TestClient

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class TestClient {

	public static void main(String[] args) {
		try {
			URL url=new URL("http://localhost:9999/ns?wsdl");
			QName sName=new QName("http://service.earl/","MyServiceImplService");
			
			Service service=Service.create(url,sName);
			IMyService ms=service.getPort(IMyService.class);
			
			System.out.println(ms.add(11,12));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

}

注:先运行MyServer,再运行TestClient。

(编辑:李大同)

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

    推荐文章
      热点阅读