有個SOA項目,原來是使用xfire,最近閒來無事於是想把它轉移到cxf上,入門級,查了很多別人寫的東西,在結合cxf的網站,E文一般般,加上猜測的成分,剛纔終於算是可以發佈到tomcat上了。記錄下正確的東西。主要是我原來的項目沒有用annotation,只是有接口和實現類,也沒用spring框架。
不過配置servlet調用cxf還是要用spring,不然可能需要寫一些別的代碼,還是把spring的包引用進來吧。
web.xml內容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
beans.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:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.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" />
<simple:server id="hw" serviceClass="demo.hw.server.HelloWorld" address="/hw">
<simple:serviceBean>
<bean class="demo.hw.server.HelloWorldImpl" />
</simple:serviceBean>
<simple:dataBinding>
<bean class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
</simple:dataBinding>
</simple:server>
</beans>
使用默認的綁定數據方式啓動tomcat的時候會報錯,所以使用aegis,這種方式不會,具體不知道爲什麼,需要繼續研究。
這樣就可以了。只是輸入地址http://127.0.0.1:9006/ra/services/hw?wsdl 出現下面的內容
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloWorld" targetNamespace="http://server.hw.demo/" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.hw.demo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://server.hw.demo/" xmlns:tns="http://server.hw.demo/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHi" type="tns:sayHi" />
- <xsd:complexType name="sayHi">
<xsd:element minOccurs="0" name="greeting" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHiResponse" type="tns:sayHiResponse" />
- <xsd:complexType name="sayHiResponse">
<xsd:element minOccurs="0" name="return" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getADocument" type="tns:getADocument" />
- <xsd:complexType name="getADocument">
<xsd:element name="getADocumentResponse" type="tns:getADocumentResponse" />
- <xsd:complexType name="getADocumentResponse">
<xsd:element minOccurs="0" name="return" nillable="true" type="xsd:anyType" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHi">
<wsdl:part element="tns:sayHi" name="parameters" />
</wsdl:message>
- <wsdl:message name="getADocumentResponse">
<wsdl:part element="tns:getADocumentResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="getADocument">
<wsdl:part element="tns:getADocument" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHiResponse">
<wsdl:part element="tns:sayHiResponse" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorldPortType">
- <wsdl:operation name="sayHi">
<wsdl:input message="tns:sayHi" name="sayHi" />
<wsdl:output message="tns:sayHiResponse" name="sayHiResponse" />
</wsdl:operation>
- <wsdl:operation name="getADocument">
<wsdl:input message="tns:getADocument" name="getADocument" />
<wsdl:output message="tns:getADocumentResponse" name="getADocumentResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorldPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHi">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHiResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="getADocument">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="getADocument">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="getADocumentResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorld">
- <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldPort">
<soap:address location="http://127.0.0.1:9006/ra/services/hw" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
還需要繼續研究的很多。今天算是有個階段性的勝利。