JAX-WS开发webservice示例详解

http://www.micmiu.com/soa/webservice/jax-ws-demo/

目录:

  • 概述
  • 实验环境
  • 服务端的实现
  • 客户端的实现

[一]、概述
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一些参考资料:

  1. JAX-RPC 2.0 renamed to JAX-WS 2.0
  2. The Java web service Tutorial 
  3. 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

1package com.micmiu.jaxws.demo;
2  
3/**
5 *
6 * @author Michael
7 */
8public interface HelloService {
9    String sayHello(String userName);
10}

实现接口并添加webservice注释:HelloServiceImpl.java

1package com.micmiu.jaxws.demo.impl;
2  
3import javax.jws.WebMethod;
4import javax.jws.WebParam;
5import javax.jws.WebService;
6import javax.jws.soap.SOAPBinding;
7  
8import com.micmiu.jaxws.demo.HelloService;
9  
10/**
12 *
13 * @author Michael
14 */
15@WebService()
16//默认SOAPBinding style = Style.DOCUMENT
17@SOAPBinding()
18public 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

1package com.micmiu.jaxws.demo;
2  
3import javax.xml.ws.Endpoint;
4  
5import com.micmiu.jaxws.demo.impl.HelloServiceImpl;
6  
7/**
9 *
10 * @author Michael
11 */
12public 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 生成wsdl文件和异常处理的相关类

ps:如果webservice中有异常声明,必须用wsgen生成常处理的相关类之后,才能发布。

按win+R键,输入cmd回车进入命令行界面,切换到target下创建目录ws,在执行如下命令:

1mkdir ws\bin ws\src ws\wsdl
  • bin    -> 存放生成的class文件
  • src    -> 存放生成的源代码文件
  • wsdl  -> 存放生成的wsdl 文件

再执行如下命令:

1wsgen -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

1package com.micmiu.jaxws.demo;
2  
3import javax.jws.WebMethod;
4import javax.jws.WebParam;
5import javax.jws.WebService;
6import javax.jws.soap.SOAPBinding;
7import javax.jws.soap.SOAPBinding.Style;
8  
9/**
11 *
12 * @author Michael
13 */
14@WebService
15@SOAPBinding(style = Style.DOCUMENT)
16public interface HelloService {
17    @WebMethod
18    String sayHello(@WebParam(name = "userName") String userName);
19}

修改接口实现类:HelloServiceImpl.java

1package com.micmiu.jaxws.demo.impl;
2  
3import javax.jws.WebService;
4  
5import com.micmiu.jaxws.demo.HelloService;
6  
7/**
9 *
10 * @author Michael
11 */
12@WebService(endpointInterface = "com.micmiu.jaxws.demo.HelloService")
13public 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目录下运行如下命令回车即可生成客户端文件:

1wsimport -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

1package com.micmiu.jaxws.client;
2  
3/**
5 * @author Michael
6 */
7public 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.

可见客户端调用成功。

 

 

========================

http://daizh868.iteye.com/blog/1194815/

使用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
Before you run this client program from any IDE or command prompt, set wlfullclient.jar in the classpath. To generate this wlfullclient.jar, please refer the notes from my below post. In Eclipse or JBuilder, you can select your project and set this library in classpath. Or in command prompt set it manually.

http://forums.oracle.com/forums/thread.jspa?messageID=3977012

Note: I already generated this wlfullclient.jar. And I DO SEE your missing class in this jar file. So the above solution should fix your problem.

Thanks
Ravi Jegga

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值