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

基于jax-ws的webservice 简单实例

发布时间:2020-12-16 23:53:13 所属栏目:安全 来源:网络整理
导读:服务器端建立 创建接口 package com.test.webservice;import javax.jws.WebService;@WebServicepublic interface TestInterface { public int add(int a,int b); public int minus(int a,int b);} 创建实现类 package com.test.webservice;import javax.jws.
  1. 服务器端建立

    1. 创建接口

      1. package com.test.webservice;
        import javax.jws.WebService;
        @WebService
        public interface TestInterface {
            public int add(int a,int b);
            public int minus(int a,int b);
        }


    2. 创建实现类

      1. package com.test.webservice;
        import javax.jws.WebService;
        @WebService(endpointInterface="com.test.webservice.TestInterface")
        public class TestImp implements TestInterface {
            public int add(int a,int b) {
                System.out.println("加法");
                return a+b;
            }
            public int minus(int a,int b) {
                System.out.println("减法");
                return a-b;
            }
        }


    3. 发布服务

      1. package com.test.webservice;
        import javax.xml.ws.Endpoint;
        public class MyService {
            public static void main(String[] args){
                String address = "http://192.168.1.105:8989/ns";
                Endpoint.publish(address,new TestImp());
            }
        }


  2. 客户端建立

    1. 使用wsimport命令生成客户端代码

      E:&;wsimport -d e:/webservice/01/ -keep -verbose http://192.168.1.105:8989/ns?ws

      dl

    2. 调用客户端代码

    3. package com.test.webservice;
      public class Client {
          /**
           * @param args
           */
          public static void main(String[] args) {
              TestImpService testImpService = new TestImpService();
              TestInterface testInterface = testImpService.getTestImpPort();
              int result = testInterface.add(1,2);
              System.out.println(result);
          }
      }

(编辑:李大同)

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

    推荐文章
      热点阅读