CXF和Spring整合
可以在传统的JavaEE应用的基础上添加一层WebService层。
我们的Java EE应用就可以对外暴露成WebService,这样就可以允许任何平台、任何语言编写程序来调用这个Java EE应用。
在传统ssh项目基础上增加WebService的步骤:
1.复制CXF的JAR包(最核心的6个)
asm、cxf-core、neethi、wsdl4j、xmlschema-core、commors-logging
*遇到错误:javax.xml.ws.soap.SOAPFaultException: Cannot create a secure XMLInputFactory 时,导入stax2-api和woodstox-core-asl两个包
2.在web.xml配置CXF的核心控制器:CXFServlet
<!-- 下面的配置表明所有来自/piranha/*请求,都交给CXFServlet处理。 -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/piranha/*</url-pattern>
</servlet-mapping>
3.在Spring配置文件中导入CXF提供的Schema、XML配置文件。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 写Location时,先写命名空间,再写物理地址 -->
4.在Spring配置文件中使用jaxws:endpoint元素来暴露WebService。
<jaxws:endpoint id="classImpl"
implementor = "org.apache.cxf.jaxws.service.Hello"
endpointName = "e:HelloEndpointCustomized"
serviceName = "s:HelloServiceCustomized"
address = "http://localhost:8080/test"
xmlns:e = "http://service.jaxws.cxf.apache.org/endpoint"
xmlns:s = "http://service.jaxws.cxf.apache.org/service"/>
最重要的是implementor 和address(不能写完整的地址)
<!-- implementor指定WebService的服务提供者。支持两种模式-->
<!--1.直接给定服务器提供者的类名;(不好)-->
<jaxws:endpoint
implementor = "com.piranha.cxf.ws.impl.HelloWorldWs"
address = "/piranha"
/>
<!-- 2.设置为容器中一个Bean。要在Bean前面加上#。 -->
<bean id="HelloWorldWs" class="com.piranha.cxf.ws.impl.HelloWorldWs"/>
<jaxws:endpoint
implementor = "#HelloWorldWs"
address = "/piranha"
/>
5.如果要添加拦截器。在jaxws:endpoint元素里添加inInterceptors、outInterceptors子元素。
<jaxws:inInterceptors>
<bean class = "com.acme.SomeInterceptor"></bean><!-- 这里是内嵌Bean -->
<ref bean = "anotherInterceptor"/><!-- 这里是引用Bean -->
</jaxws:inInterceptors>
CXF与Spring的另一种整合:
1.让Action依赖远程WebService
2.复制CXF的Jar包
3.在Spring配置文件中导入CXF提供Schema、XML配置文件
4.在Spring配置文件中使用jaxws:client元素来配置远程WebService代理
5.如果要添加拦截器。在jaxws:client元素里添加inInterceptors、outInterceptors子元素
Annotation 1@WebService(name="Example", targetNamespace="http://www.jsoso.com/wstest", serviceName="Example")
@WebService标签主要将类暴露为WebService,其中targetNamespace属性定义了自己的命名空间,serviceName则定义了< definitions >标签和<service>标签的name属性。
Annotation 2:@SOAPBinding(style=SOAPBinding.Style.RPC)
@SOAPBinding标签定义了WSDL文档中SOAP的消息协议,其中style属性对应SOAP的文档类型,可选的有RPC和DOCUMENT
Annotation 3:@WebMethod(operationName="toSayHello",action="sayHello",exclude=false)
@WebMethod定义Web Service运作的方法,
属性action 对应操作的活动 ,如<soap:operation soapAction="sayHello" />
属性operationName匹配的wsdl:operation 的名称,如<operation name="toSayHello" parameterOrder="userName">
属性exclude 用于阻止将某一继承方法公开为web服务,默认为false
Annotation 4:@WebResult(name="returnWord")
@ WebResult定义方法返回值得名称,如<part name="returnWord" type="xsd:string" />
Annotation 5:@WebParam(partName="person", mode=Mode.IN
@WebParam定义方法的参数名称,如<part name="person" type="tns:person" />,其中mode属性表示参数的流向,可选值有IN / OUT / INOUT