webservice接口的开发和调用

1、开发webservice接口的方式web

一、使用jdk开发spring

二、使用第三方工具,如cxf、shiro等tomcat

2、使用jdk开发webservice接口以及调用app

首先定义一个天气预报的接口,Weather工具

@WebService
public interface Weather {
    String queryWeather();
}

定义一个实现类,实现该接口url

@WebService
public class WeatherImpl implements Weather{
    public String queryWeather() {
        return "今日天气为晴,偏北风二到三级";
    }
}

写一个普通的类,使其继承自spring的上下文监听器,并在初始化方法中发布接口,这样在容器启动时自动会发布spa

public class MyListener extends ContextLoaderListener{

    public void contextInitialized(ServletContextEvent event) {
        String address="http://localhost:8080/weather";
        Endpoint.publish(address, new WeatherImpl());
        super.contextInitialized(event);
    }
}

在web容器中设置该监听器code

 <listener>
      <listener-class>springframe.listener.MyListener</listener-class>
  </listener>

启动容器(若是启动过程当中报错:出现相似blog

Wrapper class webservice.jaxws.SayHi is not found. Have you run APT to generate them?

则说明使用的jdk版本太低,请使用jdk1.6或更高),访问http://localhost:8080/weather,结果以下:继承

表示发布成功。

 

接下来是如何调用一个发布的webservice接口

新建一个项目test_p

选中项目,鼠标右键,选择NEW,选择other,找到web service client,next,在弹出的框中选择WSDL URL,病输入wsdl的url,这里是http://localhost:8080/weather?wsdl,next,finish

 

而后为咱们生成了一堆类

不过咱们只需用到最后两个,Weather_service和Weather

下面写一个main方法

public static void main(String[] args) {
        Weather_Service factory=new Weather_Service();
        Weather wea=factory.getWeatherImplPort();
        System.out.println(wea.queryWeather());
    }

执行,会输出以下的结果:

表明调用成功。

注意:若是webservice用到的端口最好与tomcat的访问端口不同,不然,会出现没法访问项目。

3、使用cxf开发webservice接口

该方法尚在研究中,敬请期待。。。

相关文章
相关标签/搜索