• WebService的简单Demo


    看到招聘要求要会WebService。就百度看看是如何实现的。

    测试了一下。发现使用webservice开发方法,好像方便了不少。服务端开发者只需要关注服务端就可以了。

    Demo结构

    IWebService  接口

    WebServiceImpl 实现类

    WebServicePush  测试发布类

    package demo;
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService
    public interface IWebService {
        @WebMethod
        String chaoba(String name);
        @WebMethod
        String haijun(String name);
    }
    package demo;
    
    import javax.jws.WebService;
    
    @WebService
    public class WebServiceImpl implements IWebService {
    
        @Override
        public String chaoba(String name) {
            // TODO Auto-generated method stub
            System.out.println("成功进入了超霸方法,超霸哥哥好帅哦");
            String a="aaa"+name;
            return a;
        }
    
        @Override
        public String haijun(String name) {
            System.out.println("成功进入了海军方法,海军部哥哥好棒哦");
            String a="bbb"+name;
            return a;
        }
    
    }
    package demo;
    
    import javax.xml.ws.Endpoint;
    
    /*
     * 发布类。测试
     */
    public class WebServicePush {
    
        
    public static void main(String[] args) {
        String address="http://127.0.0.1:8080/web";
        
        
        Endpoint.publish(address, new WebServiceImpl());
        System.out.println("成功");
        
        
    
    }
    
    }

    编写好以上代码后,运行。在浏览器地址栏输入

    http://127.0.0.1:8080/web?wsdl即可看到成功界面

    然后新建一个项目。在命令终端运行wsimport -s /home/chaoba/eclipseworkspace/Client/src -keep http://127.0.0.1:8080/web?wsdl
    /home/chaoba/eclipseworkspace/Client/src为新建项目地址
    http://127.0.0.1:8080/web?wsdl为发布地址
    运行后。会在新建的项目生成一些java文件

    然后新建一个test类。测试可以访问不

    package demo;
    
    public class Test {
        
        
        
        public static void main(String[] args) {
            
    
            WebServiceImplService fa=    new WebServiceImplService();  
            WebServiceImpl a=fa.getWebServiceImplPort();
            String result=a.haijun("海军");
            System.out.println(result);
            String result1=a.chaoba("超霸");
            System.out.println(result1);
        }
    
    }
    
    
    一万年太久,只争朝夕!
  • 相关阅读:
    ASP.NET 2.0 中动态添加 GridView 模板列的例子
    ASP操作Excel技术总结
    JS实现下拉列表效果
    发布无限制版CodePlusV2.0(转载)
    SQL行列转换(转载)
    [转]详解C中volatile关键字
    [转]68013开发笔记之一
    清除计算机占用串口
    modelsim保存仿真波形
    SQL存储过程分页算法研究(支持千万级) 转
  • 原文地址:https://www.cnblogs.com/chaoba/p/10099752.html
Copyright © 2020-2023  润新知