目录:
- 概述
- 实验环境
- 服务端的实现
- 客户端的实现
[一]、概述
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一些参考资料:
[二]、实验环境
- 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;
2
3
/**
4
* blog http://www.micmiu.com
5
*
6
* @author Michael
7
*/
8
public interface HelloService {
9
String sayHello(String userName);
10
}
实现接口并添加webservice注释:HelloServiceImpl.java
1
package com.micmiu.jaxws.demo.impl;
2
3
import javax.jws.WebMethod;
4
import javax.jws.WebParam;
5
import javax.jws.WebService;
6
import javax.jws.soap.SOAPBinding;
7
8
import com.micmiu.jaxws.demo.HelloService;
9
10
/**
11
* blog http://www.micmiu.com
12
*
13
* @author Michael
14
*/
15
@WebService()
16
//默认SOAPBinding style = Style.DOCUMENT
17
@SOAPBinding()
18
public class HelloServiceImpl implements HelloService {
19
20
@WebMethod
21
public String sayHello(@WebParam(name = "userName") String userName) {
22
return "hi," + userName + " welcom to www.micmiu.com";
23
}
24
25
}
编写服务端发布代码:ServerStart.java
1
package com.micmiu.jaxws.demo;
2
3
import javax.xml.ws.Endpoint;
4
5
import com.micmiu.jaxws.demo.impl.HelloServiceImpl;
6
7
/**
8
* blog http://www.micmiu.com
9
*
10
* @author Michael
11
*/
12
public class ServerStart {
13
14
/**
15
* @param args
16
*/
17
public static void main(String[] args) {
18
System.out.println("start publish jax-ws ...");
19
HelloService service = new HelloServiceImpl();
20
Endpoint.publish("http://localhost:8082/HelloService", service);
21
System.out.println("publish webservice successful");
22
}
23
24
}
运行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
可见服务端已经发布成功。
运用JDK自动的命令: wsgen
ps:如果webservice中有异常声明,必须用wsgen生成常处理的相关类之后,才能发布。
按win+R键,输入cmd回车进入命令行界面,切换到target下创建目录ws,在执行如下命令:
mkdir ws\bin ws\src ws\wsdl
- bin -> 存放生成的class文件
- src -> 存放生成的源代码文件
- wsdl -> 存放生成的wsdl 文件
再执行如下命令:
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;
2
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;
8
9
/**
10
* blog http://www.micmiu.com
11
*
12
* @author Michael
13
*/
14
@WebService
15
@SOAPBinding(style = Style.DOCUMENT)
16
public interface HelloService {
17
@WebMethod
18
String sayHello(@WebParam(name = "userName") String userName);
19
}
修改接口实现类:HelloServiceImpl.java
1
package com.micmiu.jaxws.demo.impl;
2
3
import javax.jws.WebService;
4
5
import com.micmiu.jaxws.demo.HelloService;
6
7
/**
8
* blog http://www.micmiu.com
9
*
10
* @author Michael
11
*/
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";
16
}
17
18
}
运行ServerStart 启动程序,可通过运行日志和浏览器访问wsdl文件进行验证。
[四]、客户端的实现
1. wsimport 生成客户端
按win+R键,输入cmd回车进入dos,切到target目录,然后创建目录:client、client\bin、client\src
在target目录下运行如下命令回车即可生成客户端文件:
wsimport -s client/src -d client/bin -p com.micmiu.jaxws.client http://localhost:8082/HelloService?wsdl
生成源文件目录结构如下:
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;
2
3
/**
4
* blog http://www.micmiu.com
5
* @author Michael
6
*/
7
public class HelloClient {
8
9
/**
10
* @param args
11
*/
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.");
19
}
20
21
}
运行测试程序,日志如下:
start webservice client ... send Michael to server hi,Michael welcom to www.micmiu.com test client end.
可见客户端调用成功。
========================
使用Myeclipse 8.5开发基于JAX-WS的Web service实例
=======================
https://forums.oracle.com/forums/thread.jspa?threadID=1115210
java.lang.NoClassDefFoundError: com/sun/xml/ws/spi/ProviderImpl
Hi |