微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于jax-ws的webservice 简单实例

  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);
          }
      }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐