[一]、概述
Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API。
在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。
在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。
当然 JAX-WS 也提供了一组针对底层消息进行操作的API调用,你可以通过Dispatch 直接使用SOAP消息或XML消息发送请求或者使用Provider处理SOAP或XML消息。
JAX-WS2.0 (JSR 224 )是Sun新的web services协议栈,是一个完全基于标准的实现。在binding层,使用的是the Java Architecture for XML Binding (JAXB, JSR 222 ),在parsing层,使用的是the Streaming API for XML (StAX, JSR 173 ),同时它还完全支持schema规范。
JAX-WS与JAX-RPC的区别 可参见:http://java.sun.com/xml/faq.html#JAX-WS-and-JAX-RPC-difference
JAX-WS一些参考资料:
- JAX-RPC 2.0 renamed to JAX-WS 2.0
- The Java web service Tutorial
- javax.jws.WebService
[二]、实验环境
- java version “1.6.0_18″、Eclipse3.7
- maven构建项目:mvn archetype:create -DgroupId=com.micmiu.jaxws.demo -DartifactId=jaxws-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[三]、服务端的实现
1.最基本的实例
编写接口代码:HelloService.java
1 | package com.micmiu.jaxws.demo; |
8 | public interface HelloService { |
9 | String sayHello(String userName); |
实现接口并添加webservice注释:HelloServiceImpl.java
1 | package com.micmiu.jaxws.demo.impl; |
3 | import javax.jws.WebMethod; |
4 | import javax.jws.WebParam; |
5 | import javax.jws.WebService; |
6 | import javax.jws.soap.SOAPBinding; |
8 | import com.micmiu.jaxws.demo.HelloService; |
18 | public class HelloServiceImpl implements HelloService { |
21 | public String sayHello( @WebParam (name = "userName" ) String userName) { |
22 | return "hi," + userName + " welcom to www.micmiu.com" ; |
编写服务端发布代码:ServerStart.java
1 | package com.micmiu.jaxws.demo; |
3 | import javax.xml.ws.Endpoint; |
5 | import com.micmiu.jaxws.demo.impl.HelloServiceImpl; |
12 | public class ServerStart { |
17 | public static void main(String[] args) { |
18 | System.out.println( "start publish jax-ws ..." ); |
19 | HelloService service = new HelloServiceImpl(); |
21 | System.out.println( "publish webservice successful" ); |
运行ServerStart,日志如下:
start publish jax-ws ...
2012-7-12 10:56:41 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class com.micmiu.jaxws.demo.impl.jaxws.SayHello
2012-7-12 10:56:42 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class com.micmiu.jaxws.demo.impl.jaxws.SayHelloResponse
publish webservice successful
浏览器打开:http://localhost:8082/HelloService?wsdl 回车显示如下:
![jax-ws-demo-01](https://i-blog.csdnimg.cn/blog_migrate/2483cb8539badb6ebac6c185a8653437.jpeg)
可见服务端已经发布成功。
运用JDK自动的命令: wsgen 生成wsdl文件和异常处理的相关类
ps:如果webservice中有异常声明,必须用wsgen生成常处理的相关类之后,才能发布。
按win+R键,输入cmd回车进入命令行界面,切换到target下创建目录ws,在执行如下命令:
1 | mkdir ws\bin ws\src ws\wsdl |
- bin -> 存放生成的class文件
- src -> 存放生成的源代码文件
- wsdl -> 存放生成的wsdl 文件
再执行如下命令:
1 | wsgen - cp .;classes/ -r ws/wsdl -s ws/src -d ws/bin -wsdl com.micmiu.jaxws.demo.impl.HelloServiceImpl |
生成后的目录如下:
D:\workspace_dev\jaxws-demo\target>tree /F ws
卷 work 的文件夹 PATH 列表
卷序列号为 2AF7-9BD9
D:\WORKSPACE_DEV\JAXWS-DEMO\TARGET\WS
├─bin
│ └─com
│ └─micmiu
│ └─jaxws
│ └─demo
│ └─impl
│ └─jaxws
│ SayHello.class
│ SayHelloResponse.class
│
├─src
│ └─com
│ └─micmiu
│ └─jaxws
│ └─demo
│ └─impl
│ └─jaxws
│ SayHello.java
│ SayHelloResponse.java
│
└─wsdl
HelloServiceImplService.wsdl
HelloServiceImplService_schema1.xsd
2.@WebService 指定 endpointInterface 实例
修改接口代码:HelloService.java
1 | package com.micmiu.jaxws.demo; |
3 | import javax.jws.WebMethod; |
4 | import javax.jws.WebParam; |
5 | import javax.jws.WebService; |
6 | import javax.jws.soap.SOAPBinding; |
7 | import javax.jws.soap.SOAPBinding.Style; |
15 | @SOAPBinding (style = Style.DOCUMENT) |
16 | public interface HelloService { |
18 | String sayHello( @WebParam (name = "userName" ) String userName); |
修改接口实现类:HelloServiceImpl.java
1 | package com.micmiu.jaxws.demo.impl; |
3 | import javax.jws.WebService; |
5 | import com.micmiu.jaxws.demo.HelloService; |
12 | @WebService (endpointInterface = "com.micmiu.jaxws.demo.HelloService" ) |
13 | public class HelloServiceImpl implements HelloService { |
14 | public String sayHello(String userName) { |
15 | return "hi," + userName + " welcom to www.micmiu.com" ; |
运行ServerStart 启动程序,可通过运行日志和浏览器访问wsdl文件进行验证。
[四]、客户端的实现
1. wsimport 生成客户端
按win+R键,输入cmd回车进入dos,切到target目录,然后创建目录:client、client\bin、client\src
在target目录下运行如下命令回车即可生成客户端文件:
生成源文件目录结构如下:
D:\WORKSPACE_DEV\JAXWS-DEMO\TARGET\CLIENT\SRC
└─com
└─micmiu
└─jaxws
└─client
HelloServiceImpl.java
HelloServiceImplService.java
ObjectFactory.java
package-info.java
SayHello.java
SayHelloResponse.java
2.编写客户端测试程序:HelloClient.java
1 | package com.micmiu.jaxws.client; |
7 | public class HelloClient { |
12 | public static void main(String[] args) { |
13 | HelloServiceImplService service = new HelloServiceImplService(); |
14 | HelloServiceImpl hello = service.getHelloServiceImplPort(); |
15 | System.out.println( "start webservice client ..." ); |
16 | System.out.println( "send Michael to server " ); |
17 | System.out.println(hello.sayHello( "Michael" )); |
18 | System.out.println( "test client end." ); |
运行测试程序,日志如下:
start webservice client ...
send Michael to server
hi,Michael welcom to www.micmiu.com
test client end.
可见客户端调用成功。