基于JAX-WS规范的WebService实现

1、相关介绍

        介绍Web Service需要首先了解SOA。SOA(Service-Oriented Architecture)面向服务架构是一种思想,它将应用程序的不同功能单元通过中间的契约(独立于硬件平台、操作系统和编程语言)连接起来,使得各种形式的功能单元更好的集成。WebService是SOA的一种很好的实现方式,WebService采用HTTP作为传输协议,SOAP(Simple Object Access Protocol)作为传输消息的格式。
       本文重要介绍基于JAX-WS规范的WebService实现方式。首先我们先了解什么是JAX-WS。
        JAX-WS (Java API for XML Web Services )是Java程序设计语言一个用来创建Web服务的API。在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。当然 JAX-WS 也提供了一组针对底层消息进行操作的API调用,你可以通过Dispatch 直接使用SOAP消息或XML消息发送请求或者使用Provider处理SOAP或XML消息。

【转载使用,请注明出处:http://blog.csdn.net/mahoking

2、使用一般方式实现基于JAX-WS规范的WebService

        本例以MyEclipse开发工具为例,服务器为Tomcat 6,演示实现基于JAX-WS规范的WebService的一般方式。
 第一步:打开MyEclipse,new→Web Service Project。Framework选择JAX-WS。本例项目工程命名为WebServer。
 第二步:新建包名cn.mahaochen.web.ws,在该包下建立类SayHello。

package cn.mahaochen.web.ws;

public class SayHello {

	public String sayHello(String name) {

		return "Welcome " + name + "Come here!";
	}
}

第三步:new→Other→Web Services→Web Service。Next,Java package选择我们之前建立的cn.mahaochen.web.ws,Next。这样MyEclipse会自动在cn.mahaochen.web.ws包上生成一个类SayHelloDelegate.java。

package cn.mahaochen.web.ws;

@javax.jws.WebService(targetNamespace = "http://ws.web.mahaochen.cn/", serviceName = "SayHelloService", portName = "SayHelloPort")
public class SayHelloDelegate {

	cn.mahaochen.web.ws.SayHello sayHello = new cn.mahaochen.web.ws.SayHello();

	public String sayHello(String name) {
		return sayHello.sayHello(name);
	}

}


同时在WEB-INF文件下会生成一个新的XML文件为:sun-jaxws.xml。

<?xml version = "1.0"?>
<endpoints version="2.0"
	xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
	<endpoint name="SayHelloPort" implementation="cn.mahaochen.web.ws.SayHelloDelegate"
		url-pattern="/SayHelloPort">
	</endpoint>
</endpoints>

web.xml中的内容也会发生改变。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <servlet>
  	<description>JAX-WS endpoint - SayHelloService</description>
  	<display-name>SayHelloService</display-name>
  	<servlet-name>SayHelloService</servlet-name>
  	<servlet-class>
  		com.sun.xml.ws.transport.http.servlet.WSServlet
  	</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SayHelloService</servlet-name>
  	<url-pattern>/SayHelloPort</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
  	<listener-class>
  		com.sun.xml.ws.transport.http.servlet.WSServletContextListener
  	</listener-class>
  </listener>
  </web-app>


第三步:将该项目发布(部署)到Tomcat中,启动服务,通过浏览器访问http://127.0.0.1/WSServer/SayHelloPort?wsdl测试,会出现如下报文。

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.web.mahaochen.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.web.mahaochen.cn/" name="SayHelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.web.mahaochen.cn/" schemaLocation="http://127.0.0.1:80/WSServer2/SayHelloPort?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="SayHelloDelegate">
<operation name="sayHello">
<input message="tns:sayHello"></input>
<output message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="SayHelloPortBinding" type="tns:SayHelloDelegate">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="SayHelloService">
<port name="SayHelloPort" binding="tns:SayHelloPortBinding">
<soap:address location="http://127.0.0.1:80/WSServer2/SayHelloPort"></soap:address>
</port>
</service>
</definitions>


3、使用注解/注释的方式实现基于JAX-WS规范的WebService

       “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注解/注解来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发步骤。
       本案例涉及的主要注解/注释类如下:

使用方式作用
javax.jws.WebService@WebService当实现 Web Service 时,@WebService 注释标记 Java 类;实现 Web Service 接口时,标记服务端点接口(SEI)。
javax.jws.WebMethod@WebMethod@WebMethod 注释表示作为一项 Web Service 操作的方法。
将此注释应用于客户机或服务器服务端点接口(SEI)上的方法,或者应用于 JavaBeans 端点的服务器端点实现类。
javax.jws.WebParam@WebParam@WebParam 注释用于定制从单个参数至 Web Service 消息部件和 XML 元素的映射。
将此注释应用于客户机或服务器服务端点接口(SEI)上的方法,或者应用于 JavaBeans 端点的服务器端点实现类。


除以上的注解/注释类还包括javax.jws.Oneway、 javax.jws.WebResult、javax.jws.HandlerChain、javax.jws.SOAPBinding等等。
【详细说明请阅读如下地址】

http://www-01.ibm.com/support/knowledgecenter/SSAW57_6.1.0/com.ibm.websphere.wsfep.multiplatform.doc/info/ae/ae/rwbs_jaxwsannotations.html?lang=zh

【转载使用,请注明出处:http://blog.csdn.net/mahoking

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hoking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值