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

jws 发布WebService

发布时间:2020-12-17 00:16:02 所属栏目:安全 来源:网络整理
导读:1: package test;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebServicepublic interface MyService {@WebResult(name = "addResult")public int add(@WebParam(name = "a") int a,@WebParam(name = "b") int b);

1:

package test;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface MyService {
	@WebResult(name = "addResult")
	public int add(@WebParam(name = "a") int a,@WebParam(name = "b") int b);

	@WebResult(name = "minusResult")
	public int minus(@WebParam(name = "a") int a,@WebParam(name = "b") int b);

}
2:

package test;

import javax.jws.WebService;

@WebService(endpointInterface="test.MyService")
public class MyServiceImpl implements MyService {

	@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;

	}

}
3:发布

package test;

import javax.xml.ws.Endpoint;


public class Service  {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 发布一个WebService
		Endpoint.publish("http://localhost:8080/sunyh_service",new MyServiceImpl());
//		System.exit(0);

	}

}
4:调用

package test;

import java.net.URL;

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

public class TestClient {

	/**
	 * @param args
	 * @throws Throwable
	 */
	public static void main(String[] args) throws Throwable {
		// wsimport -p test http://localhost:8080/sunyh_service?wsdl
		URL url = new URL("http://localhost:8080/sunyh_service?wsdl");

		QName qName = new QName("http://test/","MyServiceImplService");

		Service service = Service.create(url,qName);

		MyService  myService = service.getPort(MyService .class);

		System.out.println("执行成功 结果为"+myService.add(12,1));

	}

}

(编辑:李大同)

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

    推荐文章
      热点阅读