WebService,顾名思义就是基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求。从而实现远程调用。
环境是Window10、JDK1.8、Maven3.3、Eclipse
DEMO: 客户端输入姓名,服务端查询并返回用户所在地
那么如何实现这样一个WebService呢?
@WebService
该注解用于对接口,类进行注解,表示要发布的web服务
@WebMethod
该注解用于用@WebService注解的类或接口的方法上,表示要发布的方法
我们要实现一个简单的WebService, 可以依赖以上两个注解。
新建一个接口LocationService,如下:
public interface LocationService {
String location(String name);
}
接下来我们建一个类实现LocationService
@WebService
public class LocationServiceImpl implements LocationService {
@WebMethod
public String location(String name) {
Map<String, String> userLoc = getAllUserLocation();
String location = userLoc.get(name);
return location == null ?
"没找到\"".concat(name).concat("\"所在地") : location;
}
Map<String, String> getAllUserLocation(){
Map<String, String> userLoc = new HashMap<>();
userLoc.put("张三", "北京市xxx");
userLoc.put("李四", "上海市xxx");
userLoc.put("王五", "深圳市xxx");
userLoc.put("大胖", "南京市xxx");
userLoc.put("二胖", "南京市xxx");
userLoc.put("三胖", "南京市xxx");
userLoc.put("小明", "南京市xxx");
return userLoc;
}
}
服务实现了,我们需要将这个服务发布出来,这样子客户端就可以调用了。
那么我们建一个发布类LocationPushish
public class LocationPublish {
public static void main(String[] args) {
String address = "http://localhost:8888/location";
Endpoint.publish(address, new LocationServiceImpl());
System.out.println("Location发布成功!");
}
}
运行main方法,浏览器访问 http://localhost:8888/location?wsdl 就可以看到发布的Web服务信息了
服务发布了,客户端该怎么调用这个服务呢?
在这里,我们使用jdk自带的wsimport来实现,通过wsimport可以生成客户端代码,下面我们先来了解一下wsimport的常用用法
wsimport [options] WSDL_URI
1. -d <directory>
在指定的目录生成class文件
2. -clientjar <jarfile>
在当前目录生成jar文件,结合-d <directory>可以在指定的目录生成jar文件
3. -s <directory>
在指定的目录生成java源文件
4. -p <pkg>
指定生成文件的包结构
5. -keep
在生成class文件,或者jar包时,同时保留java源文件
方式一:生成客户端源码,将源码拷贝到客户端使用
wsimport -s D:\demo http://localhost:8888/location?wsdl
方式二:生成jar包,客户端引入使用
wsimport -d D:\demo -clientjar location.jar http://localhost:8888/location?wsdl
本文我们使用第二种方式来演示,首先将生成的jar引入客户端代码
这里介绍两种方式来实现在Maven Project中引入jar包
方法一:
将jar包放置src/main/webapp/WEB-INF/lib/目录下,在pom.xml中添加以下引用
<dependency>
<groupId>com.macrowing</groupId>
<artifactId>location</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>
${project.basedir}/src/main/webapp/WEB-INF/lib/location.jar
</systemPath>
</dependency>
方法二:
进入jar包所在目录,执行以下命令
mvn install:install-file -Dfile=location.jar -DgroupId=com.macrowing -DartifactId=location -Dversion=1.0.0 -Dpackaging=jar
在pom.xml中添加以下引用
<dependency>
<groupId>com.macrowing</groupId>
<artifactId>location</artifactId>
<version>1.0.0</version>
</dependency>
jar包引入完之后,我们在客户端新建一个类LocationClient来测试
public class LocationClient {
public static void main(String[] args) {
//获取到location服务
LocationServiceImplService factory =
new LocationServiceImplService();
LocationServiceImpl locService=
factory .getLocationServiceImplPort();
String res = locService.location("张三");
System.out.println("张三所在地:".concat(res ));
res = locationServiceImpl.location("李四");
System.out.println("李四所在地:".concat(res ));
res = locService.location("皮皮虾");
System.out.println("皮皮虾所在地:".concat(res ));
}
}
到这里就结束了,运行main方法将得到WebService返回结果。
源码地址:https://github.com/biaotang/demo-webservice/