`

JAX-WS实现Web Service

    博客分类:
  • Java
 
阅读更多

一、为什么使用Web Service?

       Java系统相互间可以通过RMI实现远程过程调用,但是由于其局限于只能用于java系统间进行调用,且开发过程相对来说较复杂,已经逐步被Web Service取代。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

二、Web Service涉及的主要技术

1、SOAP:Simple Object Access Protocol

SOAP 指简单对象访问协议,它是一种基于XML的消息通讯格式,用于网络上,不同平台,不同语言的应用程序间的通讯。可自定义,易于扩展。一条 SOAP 消息就是一个普通的 XML 文档。通过HTTP或者SMTP等应用层协议进行通讯,自身使用XML文件来描述程序的函数方法和参数信息,从而完成不同主机的异构系统间的计算服务处理。

2、WSDL:

    采用XML编写的,用于描述服务的位置和方法的文档。

3、XML

三、JAX-WS

JAX-WS(Java API for XML-Based Webservices)是目前最标准,需要额外第三方插件最少,配置最少最灵活的webservice。JAXWS适合几乎所有Webservice客户端的调用,因此不少巨头型的厂商如:IBM,Weblogic等,在他们的产品上都使用了以JAXWS为标准的Webservice接口。JAX-WS 2.0 是面向 Java 5 的开发 Web services 的最新编程标准,它提供了新的编程模型和对以往的 JAX-RPC 方式的 Web services 进行了增强。 JAX-WS2.0 (JSR 224)是Sun新的web services协议栈,是一个完全基于标准的实现。

1、两种开发过程

     JAX-WS有两种开发过程:    

          自顶向下,通过WSDL文件来创建Web Service;

          自底向上,通过Java类来创建Web Service;

2、开发步骤

     JAX-WS的开发基本上采用如下步骤:

            编写接口和实现-----》发布-------》生成客户端调用程序

     采用JDK1.6时,可以编写接口;也可以不编写接口,直接使用JAVA类(其他JDK版本未测试)

3、关于 wsgen    wsimport

     JDK1.6(具体详细版本待查)已经提供该工具,可以直接使用。

     wsgen用于server端通过java类产生webservice和wsdl文件;

     wsimport用于client端通过wsdl文件生成java类;

     wsgen使用:

            a、javac编译java类;

            b、wsgen -cp . java类      执行后会看到生成的wsdl文件

     wsimport使用:

            wsimport -keep -p package-name    http://xxx/helloservice?wsdl

            -keep   保留生成的文件;

            -p 指定生成时的包名;

            指定服务地址

4、生成客户端的方式

      a、可以通过myeclipse工具生成;

      b、通过wsimport生成

5、注意事项

      a、jdk1.5不能直接使用JAXWS,需要导入相应的jar包;

      b、jdk1.5没有自带wsgen、wsimport,需要JAXWS-xxx.jar(通过java -jar 进入图形界面、解压获取)

     

四、示例

1、接口方式实现

           

@WebService
@SOAPBinding(style = Style.RPC)
public interface IHello {
	@WebMethod
	public void sayHello();

	@WebMethod
	public void sayHello2(@WebParam(name = "name") String name);

	@WebMethod
	@WebResult(name = "checkresult")
	public int checkExist(@WebParam(name = "name") String name);
}

 

@WebService(endpointInterface = "com.test.IHello")
@SOAPBinding(style = Style.RPC)
public class Hello implements IHello {
	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.println("hello");
	}

	public void sayHello2(String name) {
		// TODO Auto-generated method stub
		System.out.println("hello " + name);
	}

	public int checkExist(String name) {
		// TODO Auto-generated method stub
		System.out.println("hello " + name);
		return 0;
	}

}

 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Endpoint endPoint=Endpoint.publish("http://localhost:8080/hello", new Hello());
	}

 

2、非接口方式(直接使用JAVA类)

@WebService( name = "hello", targetNamespace = "http://www.xx.com",serviceName="helloservice")
@SOAPBinding(style = Style.RPC)
public class Hello  {
	@WebMethod(operationName = "sayHello", action = "sayHelloaction")
	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.println("hello");
	}

	@WebMethod
	public void sayHello2(String name) {
		// TODO Auto-generated method stub
		System.out.println("hello " + name);
	}

	@WebMethod
	@WebResult(name = "checkresult")
	public int checkExist(@WebParam(name = "name") String name) {
		// TODO Auto-generated method stub
		System.out.println("hello " + name);
		return 0;
	}

}

 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Endpoint endPoint=Endpoint.publish("http://localhost:8080/hello", new Hello());
	}

 

3、Web项目中实现JAX-WS

      具体服务的编写与前面的叙述是一致的,只是不再采用main方法实现发布,而是通过web项目配置文件进行发布。另外需要注意在web项目中导入所需的jar包(myeclipse自带该部分jar包,可以直接用)。

/WEB-INF/sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
	version="2.0">
	<endpoint name="Hello" implementation="com.test.Hello"
		url-pattern="/HelloService" />
</endpoints>

 

/WEB-INF/web.xml
	<listener>
		<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>Hello</servlet-name>
		<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/HelloService</url-pattern>
	</servlet-mapping>

 

 4、CXF方式实现Web Service

Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。目前它仍只是 Apache 的一个孵化项目。

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成

	非WEB应用,只需引入cxf的jar包,然后采用如下方式发布服务:

     public static void main(String[] args) {
		// TODO Auto-generated method stub
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		factory.setServiceClass(Hello.class);

		factory.setAddress("http://localhost:9000/ws/HelloWorld");
		factory.create();
	}

 

对于使用Spring的web应用程序,需要在配置文件中做如下处理:

applicationContext.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath*:**META-INF/cxf/cxf.xml" />
	<import resource="classpath*:**META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath*:**META-INF/cxf/cxf-servlet.xml" />
	<jaxws:endpoint id="helloworld" implementor="com.common.Hello"	address="/Hello" />

</beans>

web.xm中做如下配置:
	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:**/applicationContext.xml</param-value>
	</context-param>

 

 

五、参考

1. wsdl binding理解

wsdl提供了一个统一的接口, 因为wsdl是基于xml,与语言平台无关, 所有可以通过wsdl提供的接口可以访问不同类型的资源。
wsdl提供了binding和service元素,用以绑定接口到具体的服务,实现了接口与实现的分离。
wsdl绑定的协议可以是soap、http、smtp、ftp等任何一种传输协议, wsdl保持协议中立,但它确实内建了绑定SOAP的支持,从而同SOAP建立了不可分割的关系, 所以通常都会把SOAP作为绑定协议。  
2. wsdlsoap:operation/soap:operation元素和wsdlsoap:binding/soap:binding元素
上面说通常都会把SOAP作为绑定协议, 所有我们目前基本上都使用SOAP binding,
就是说wsdlsoap:binding/soap:binding这个元素以及子元素wsdlsoap:operation/soap:operation必须存在;
3. rpc和document区别
style属性是用来描述soap:operation的,这个值只有两个选择,RPC-oriented(简称rpc)或者document-oriented(简称document).
rpc表示soap消息包含请求的参数和返回的值, document表示soap消息包含xml document(s).
4. style属性规则
如果wsdlsoap:operation/soap:operation元素中有style属性, 那么这个operation就应用这个style类型,
如果没有,就需要找上一级wsdlsoap:binding/soap:binding元素中有没有style属性, 如果有的话,那么该binding下的所有operation都应用这个style类型,
如果没有,就应用默认值style="document"

 

 

关于SOAP、WSDL的介绍参考:SOAP WSDL介绍
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics