概述
这里提供一个最简单的Web Service的实现,基于JAX-WS。除了jdk不需要任何其他jar包,使用Eclipse提供的Web Services Explorer访问服务。
服务端的实现
package cn.ljl.sand.jws.chapter0.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class HelloService { public String hello(String name) { return "Hello," + name; } public static void main(String[] args) { String address = "http://localhost:6666/service/hello"; Endpoint.publish(address, new HelloService()); } }
这里类有两个地方值得注意:
- WebService注解
这个注解将这个类声明为Web Service - main方法
使用Endpoint的publish方法发布服务
通过Eclipse的工具进行访问
Eclipse内嵌了Web Services Explorer工具,可以作为客户端,访问我们上面发布的服务。
1. 在Eclipse设置web浏览器
Web Services Explorer需要使用浏览器打开,可以使用Eclipse内嵌的浏览器,或者其他外部的浏览器。
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111329390-1560305876.png)
我这里使用了谷歌浏览器,后来发现无法看到SOAP报文,又切换了IE浏览器。
2. 打开工具
Run - Launch the Web Services Explorer
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111330000-703926817.png)
3. 请求服务
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111330609-1292295200.png)
这里的wsdl的地址,是我们在服务端发布的地址,加上
?wsdl
。![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111331250-2009803863.png)
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111331828-991582095.png)
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111332297-540844063.png)
![](https://images2015.cnblogs.com/blog/721161/201601/721161-20160123111333047-207809463.png)