WebService笔记(第二弹:使用JAX-WS开发WebService)

我们现在先来使用JDK的JAX-WS来开发一个WebService。
JAX-WS规范是一组XML web services的JAVA API。JAX-WS允许开发者可以选择RPC-oriented或者message-oriented 来实现自己的web services。
在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP。在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码。JAX-WS的运行时实现会将这些API的调用转换成为对于SOAP消息。
在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI (service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。
在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。
通过web service所提供的互操作环境,我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作。

定义服务端WebService

我们使用JDK的JAX-WS定义WebService,需要使用@WebService注解在WebService类的接口上进行标记。我们先来定义一个叫做MyWS的WebService接口。

package com.hello.ws;

import javax.jws.WebService;

@WebService
public interface MyWS {

    public String sayHello(String name);
}

对应的实现类如下:

package com.hello.ws;

import javax.jws.WebService;

@WebService
public class MyWSImpl implements MyWS {

    @Override
    public String sayHello(String name) {
        return "I am web service, hello " + name;
    }

}

发布服务端WebService

我们使用Endpoint类来进行发布。

package com.hello.ws;

import javax.xml.ws.Endpoint;

public class MyEndPoint {

    public static void main(String[] args) {
        String address = "http://localhost:8089/ws/MyWS";
        Endpoint.publish(address, new MyWSImpl());
    }

}

运行一下上面的这段代码,控制台并没有什么输出,但是我们可以在用浏览器访问:http://localhost:8089/ws/MyWS?wsdl ,我们可以看到:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.hello.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.hello.com/" name="MyWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.hello.com/" schemaLocation="http://localhost:8089/ws/MyWS?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="MyWSImpl">
<operation name="sayHello">
<input wsam:Action="http://ws.hello.com/MyWSImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://ws.hello.com/MyWSImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="MyWSImplPortBinding" type="tns:MyWSImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyWSImplService">
<port name="MyWSImplPort" binding="tns:MyWSImplPortBinding">
<soap:address location="http://localhost:8089/ws/MyWS"/>
</port>
</service>
</definitions>

生成WebService客户端代码

我们要使用jdk的wsimort.exe工具生成客户端代码,wsimort.exe工具位于Jdk的bin目录下。
我们新建一个简单的WebService客户端工程,打开cmd命令行窗口,cd到新建出来的客户端工程的src目录,执行命令:“wsimport -keep http://localhost:8089/ws/MyWS?wsdl”生成客户端代码。我们在src目录下可以看到生成出来的客户端代码。

在客户端调用WebService对外提供的方法

wsimport工具帮我们生成了好几个.java和.class文件,但我们只需要关心MyWSImplService类和MyWSImpl接口的使用即可。

package com.hello.ws;

public class WSClient {

    public static void main(String[] args) {
        // 创建一个用于产生MyWSImpl实例的工厂,MyWSImplService类是wsimport工具生成的
        MyWSImplService factory = new MyWSImplService();
        // 通过工厂生成一个MyWSImpl实例,MyWSImpl是wsimport工具生成的
        MyWSImpl impl = factory.getMyWSImplPort();
        // 调用WebService的方法
        String result = impl.sayHello("wsclient");
        System.out.println(result);
    }

}

运行一把这段代码,可以看到控制台输出:
I am web service, hello wsclient
这说明,wsimport工具生成的客户端代码已经成功调用到了服务端WebService中的方法。

以上,我们就实现了使用JAX-WS开发WebService。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值