zoukankan      html  css  js  c++  java
  • webService服务简单实现

    首先写一个简单的webservice服务

    package com.service.impl;
    
    import java.util.Date;
    
    import javax.jws.WebService;
    
    import com.service.HelloWorld;
    
    
    @WebService
    public class HelloWorlds implements HelloWorld{
    
        @Override
        public String sayHi(String name) {
            // TODO Auto-generated method stub
             return name+"您好!现在时间是:"+new Date();  
        }
    }

    接下来就是将weibservice服务发布暴露出来

    package com.action;
    
    import javax.xml.ws.Endpoint;
    
    import com.service.HelloWorld;
    import com.service.impl.HelloWorlds;
    
    public class ServiceMain {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            HelloWorld hw = new HelloWorlds();  
            //调用Endpoint的publish方法发布Web Service  
            Endpoint.publish("http://127.0.0.1/helloWorld", hw);  
            System.out.println("Web Service暴露成功!");  
        }
    
    }

    通过访问http://127.0.0.1/helloWorld?wsdl就可以看到服务的描述,也就是webservice三大组件中的wsdl.

    接下来就是创建webservice服务的客户端用来调用服务,记住在创建的时候在WSDL URL输入上面查看描述服务的地址就可以自动生成对应的webservice客户端

    我的生成的文件如下

    最后就是写测试程序开始调用webservice发布的服务了

    package com.action;
    
    
    
    import com.service.impl.HelloWorlds;
    import com.service.impl.HelloWorldsService;
    public class WebService {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub        
            HelloWorldsService service=new HelloWorldsService();
            HelloWorlds action=service.getHelloWorldsPort();
            String message=action.sayHi("JIM");
            System.out.println("调webservice:"+message); 
        }
    
    }
  • 相关阅读:
    页面渲染速度增加的方法和建议
    五(六)、Package & import
    五(五)、构造器 & JavaBean &this
    五(四)、封装性
    五(三)、方法
    五(二)、匿名对象
    五(一)、类&对象概述
    六、java 异常处理
    四、java 数组
    三、java 基础提炼
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/6865829.html
Copyright © 2011-2022 走看看