WebService

发布时间 2023-07-31 12:14:18作者: 天晴修屋顶

1. 什么是Webservice

WebService(Web服务)是一种基于标准化的互联网通信协议和技术,用于在网络上进行机器之间的通信和数据交换。它提供了一种跨平台、跨语言的方式,让不同系统之间能够相互交互和共享数据。

WebService通常使用基于HTTP(如SOAP和REST)的协议来传输数据。以下是一些与WebService相关的关键概念:

SOAP (Simple Object Access Protocol):简易对象访问协议,soap用来描述传递信息的格式。

WSDL (WebServices Description Language):Web服务描述语言,用来描述如何访问具体的接口。

UDDI (Universal Description Discovery and Integration):通用描述、发现及整合,用来管理、分发、查询webService。

SEI(WebService Endpoint Interface) webservice的服务调用终端接口

2. 构建一个WebService程序

目录结构如下

2.1 创建实体

 1 public class Weather {
 2   private String city;
 3   private double temperature;
 4 
 5   public String getCity() {
 6     return city;
 7   }
 8 
 9   public void setCity(String city) {
10     this.city = city;
11   }
12 
13   public double getTemperature() {
14     return temperature;
15   }
16 
17   public void setTemperature(double temperature) {
18     this.temperature = temperature;
19   }
20 }

 

2.2 创建一个接口和对应的实现类,并在这个实现类上面使用@WebService注解(代表是要被暴露的服务)【注意:标注后类中的使用public修饰非静态的方法将会被暴露给客户端调用】

1 public interface WeatherService {
2     String getWeatherInfoByCityName(String cityName);
3     String getWeatherInfo(Weather weather);
4 }
 1 import javax.jws.WebService;
 2 
 3 /*
 4   Author:Mike
 5   创建时间:2019/11/3
 6   描述:
 7 */
 8 @WebService
 9 public class WeatherServiceImpl implements WeatherService {
10     @Override
11     public String getWeatherInfoByCityName(String cityName) {
12         return "获取"+cityName+"的天气信息!";
13     }
14 
15     @Override
16     public String getWeatherInfo(Weather weather) {
17         return "获取"+weather.getCity()+"的天气信息!\n温度:"+weather.getTemperature()+"℃";
18     }
19 }

2.3 在服务端主类中暴露服务

 1 import com.ws.nav_server.demo.WeatherServiceImpl;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 /*
 6   Author:Mike
 7   创建时间:2019/11/3
 8   描述:
 9 */
10 public class App {
11     public static void main(String[] args) {
12         Endpoint.publish("http://localhost:8090/weatherinfo", new WeatherServiceImpl());
13         System.out.println("发布成功!");
14     }
15 }

2.4 运行项目,查看http://localhost:8090/weatherinfo?wsdl 是否成功暴露接口信息

 至此服务端已经完成,接着看客户端如何调用

3. 生成java文件

使用jdk自带工具wsimport生成客户端调用的服务

wsimport [options] <WSDL_URI>

比较常用的[options]有:

  1. -d <directory>

    在指定的目录生成class文件

  2. -clientjar <jarfile>

    在当前目录生成jar文件,结合-d <directory>可以在指定的目录生成jar文件

  3. -s <directory>

    在指定的目录生成java源文件

  4. -p <pkg>

    指定生成文件的包结构

  5. -keep

    在生成class文件,或者jar包时,同时保留java源文件

3.1 先运行服务,然后使用以下命令生成java文件

1 wsimport -s D:\workspace\study\webserver\nav\webserver-demo\wsdl http://localhost:8090/weatherinfo?wsdl -encoding UTF-8

3.2 创建client项目,将生成的java文件放置目录中

 3.3 调用远程的方法

 1 import com.ws.nav_server.demo.Weather;
 2 import com.ws.nav_server.demo.WeatherServiceImpl;
 3 import com.ws.nav_server.demo.WeatherServiceImplService;
 4 
 5 /*
 6   Author:Mike
 7   创建时间:2019/11/4
 8   描述:
 9 */
10 public class WeatherTest {
11     public static void main(String[] args) {
12         WeatherServiceImplService implService = new WeatherServiceImplService();
13         WeatherServiceImpl weatherImpl = implService.getWeatherServiceImplPort();
14         String weatherInfo = weatherImpl.getWeatherInfoByCityName("广州");
15         System.out.println(weatherInfo);
16 
17         System.out.println("==========================");
18         Weather weather = new Weather();
19         weather.setCity("北京");
20         weather.setTemperature(22d);
21         weatherInfo = weatherImpl.getWeatherInfo(weather);
22         System.out.println(weatherInfo);
23     }
24 }

4 运行结果