首页 > 代码库 > 你真的了解JAVA中与Webservice相关的规范和实现吗?
你真的了解JAVA中与Webservice相关的规范和实现吗?
非常多人在项目中使用Webservice,仅仅是知道怎样公布Webservice,怎样调用Webservice,但真要论其来龙去脉,还真不一定清楚。
一切一切还要从我们伟大的sun公司规范说起。
JAVA 中共同拥有三种WebService 规范,各自是JAXM&SAAJ、JAX-WS(JAX-RPC)、JAX-RS。
以下来分别简要的介绍一下这三个规范。针对JDK 1.6以下版本号,新的还没研究过
(1.)JAX-WS:
JAX-WS(Java API For XML-WebService),JDK1.6 自带的版本号为JAX-WS2.1,其底层支持为JAXB。早期的基于SOAP 的JAVA 的Web 服务规范JAX-RPC(Java API For XML-Remote Procedure Call)眼下已经被JAX-WS 规范代替,JAX-WS 是JAX-RPC 的演进版本号,但JAX-WS 并不全然向后兼容JAX-RPC,二者最大的差别就是RPC/encoded 样式的WSDL,JAX-WS 已经不提供这样的支持。JAX-RPC 的API 从JAVA EE5 開始已经移除,如果你使用J2EE1.4,其API 位于javax.xml.rpc.*包。JAX-WS(JSR 224)规范的API 位于javax.xml.ws.*包,当中大部分都是注解,提供API 操作Web 服务(通常在client使用的较多,因为client能够借助SDK 生成,因此这个包中的API 我们较少会直接使用)。WS-MetaData(JSR 181)是JAX-WS 的依赖规范,其API 位于javax.jws.*包,使用注解配置公开的Web 服务的相关信息和配置SOAP 消息的相关信息。
(2.)JAXM&SAAJ:
JAXM(JAVA API For XML Message)主要定义了包括了发送和接收消息所需的API,相当于Web 服务的server端,其API 位于javax.messaging.*包,它是JAVA EE 的可选包,因此你须要单独下载。
SAAJ(SOAP With Attachment API For Java,JSR 67)是与JAXM 搭配使用的API,为构建SOAP 包和解析SOAP 包提供了重要的支持,支持附件传输,它在server端、client都须要使用。这里还要提到的是SAAJ 规范,其API 位于javax.xml.soap.*包。
JAXM&SAAJ 与JAX-WS 都是基于SOAP 的Web 服务,相比之下JAXM&SAAJ 暴漏了SOAP很多其它的底层细节,编码比較麻烦,而JAX-WS 更加抽象,隐藏了很多其它的细节,更加面向对象,实现起来你基本上不须要关心SOAP 的不论什么细节。那么假设你想控制SOAP 消息的很多其它细节,能够使用JAXM&SAAJ。
(3.)JAX-RS:
JAX-RS 是JAVA 针对REST(Representation State Transfer)风格制定的一套Web 服务规范,因为推出的较晚,该规范(JSR)并未随JDK1.6 一起发行,你须要到JCP(Java Community Process) 上单独下载JAX-RS 规范的接口,其API 位于javax.ws.rs.*包。
对AXIS没有太大发言权,由于用得少,这里还是说一点关于CXF的吧。(參考官方文档)
Apache CXF is an open source services framework.CXF helps you build and develop services using frontend programming APIs, likeJAX-WS and JAX-RS. These services can speak a variety of protocols such asSOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transportssuch as HTTP, JMS or JBI.
假设你想在maven中使用cxf,那么要在pom中声明maven依赖。
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-distribution-manifest</artifactId>
<version>2.5.9</version>
<scope>provided</scope>
</dependency>
经測试,调用Webservice,仅仅须要引这一个就够了,它会间接引入一些其它jar
最后说下,Cxf_home 2.5.9版本号的lib下jar的作用:
The following jars are required for all CXFusage:
- cxf.jar
- commons-logging.jar
- geronimo-activation.jar (Or the Sunequivalent) [6]
- geronimo-annotation.jar (Or the Sunequivalent) [6]
- geronimo-javamail.jar (Or the Sunequivalent) [6]
(MAY be able to remove javamail if you don‘t use any MTOM/SAAJ typethings)
- geronimo-stax-api.jar (Or the Sunequivalent) [6]
- neethi.jar
- jaxb-api.jar [6]
- jaxb-impl.jar (this is a [6] for normalruntime, but is required for
tooling and the dynamicclients)
- XmlSchema.jar
- woodstox-core-asl.jar [6] or another StAX implementation
- stax2-api-3.0.1.jar [6] for woodstoxabove
- wsdl4j.jar
The following jars are required for XMLcatalog support:
- xml-resolver.jar
For Java2WSDL and WSDL2Java:
- The above jars
- jaxb-xjc.jar
- velocity.jar
- commons-collections.jar
- commons-lang.jar
- cxf-xjc*.jar (optional, these are xjcextensions for providing enhanced
JAXB code generation)
For JAX-WS support:
- geronimo-ws-metadata.jar [6]
- geronimo-jaxws_2.2_spec-1.0.jar (Or theSun equivalent) [6]
- saaj-api.jar [6]
- saaj-impl.jar [6]
- asm.jar (semi-optional, helps withperformance of wrapper types and is
required when adding JAXB annotations onto the SEI methods and
parameters.)
For XML Configuration support:
- aopalliance.jar
- spring-beans.jar
- spring-context.jar
- spring-core.jar
- spring.web.jar
- FastInfoset.jar
For standalone HTTP support:
- geronimo-servlet.jar
- jetty-*.jar
- sl4j.jar & sl4j-jdk14.jar (optional -but improves logging)
For Aegis support:
- jdom.jar (optional, if you want to mapxsd:anyType to JDOM)
For XmlBeans databinding support:
- xmlbeans.jar
For WS-Security support:
- bcprov-jdk15.jar
- xalan.jar
- serializer.jar
- wss4j.jar
- xmlsec.jar
- ehcache-core.jar
For SAML support in WS-Security
- joda-time.jar
- opensaml.jar
- openws.jar
- xmltooling.jar
For HTTP Binding support:
- jra.jar
- jettison.jar (Needed for JSON servicesonly)
For JAX-RS support:
- abdera* (optional: for AtomPub support, but also needed for the
Management log browser console)
- commons-codec.jar (Needed for Abdera)
- jaxen.jar (Needed for Abdera)
- axiom* (Needed for Abdera)
- jsr311-api.jar
- jettison.jar (Needed for JSON servicesonly)
- oauth*.jar (Needed for OAuth support)
For JMS transport
- geronimo-jms.jar (Or the Sun equivalent)
- spring-jms.jar
- spring-tx.jar
For CORBA support:
- antlr.jar
For JavaScript functionality:
- js.jar
For schema validation with the Aegis databinding.
-msv-core.jar (version 2010.1)
-xsdlib:jar (version 2010.1)
-isorelax.jar (version 20030108)
-relaxngDatatype.jar (version 20020414)
For the WS-Notification Service:
-cxf-services-wsn-api.jar
-cxf-services-wsn-core.jar
For the CXF Security Token Service:
- cxf-services-sts-core.jar人家官方文档,写得那么好,我就不再嚼舌头了,大家去官网看吧。
了解了java Webservice这块的规范和实现,相信你再次在java中使用Webservice就不会那么糊涂了,该怎么做,出了问题怎么办,心中有数了吧。