Mavne + Spring整合CXF
-
Eclipse创建下Mavne + Spring整合CXF
1.创建工程:file->new->MavenProject(勾选:create a simple project)点击Next_>输入相应的工程属性后点击完成(finished)
属性如下:
<groupId>gdsbcw.cxf</groupId>
<artifactId>gdsbcw-cxf</artifactId>
<version>01</version>
<packaging>war</packaging>
<name>gdsbcw_cxf</name>
<description>gdsbcw_cxf</description>
- 生产的工程结构如下:
2.创建包:package:gdsbcw_cxf
3.在gdsbcw_cxf下要发布的接口:BankTransaction
package gdsbcw_cxf; import javax.jws.WebParam; import javax.jws.WebService; @WebService(name="BankTransaction") public interface BankTransaction { public String getAesSeed( @WebParam( name="sid") String sid); public String synAesSeed( @WebParam( name="sid") String sid, @WebParam(name="seed") String seed); public String requestBank(@WebParam( name="sid") String sid, @WebParam(name="xml") String xml); }
3-1说明:
@WebService:将该接口发布成WebService服务器
4.创建实现BankTransaction的class:BankTransactionIMplementation
package gdsbcw_cxf; import javax.jws.WebService; @WebService(targetNamespace="http://gdsbcw_cxfxzh/") public class BankTransactionIMplementation implements BankTransaction { @Override public String getAesSeed(String sid) { // TODO Auto-generated method stub return null; } @Override public String synAesSeed(String sid, String seed) { // TODO Auto-generated method stub return null; } @Override public String requestBank(String sid, String xml) { // TODO Auto-generated method stub return null; } }
5.编辑pom.xml文件,引入所需jar文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>gdsbcw.cxf</groupId> <artifactId>gdsbcw-cxf</artifactId> <version>01</version> <packaging>war</packaging> <name>gdsbcw_cxf</name> <description>gdsbcw_cxf</description> <url>http://maven.apache.org</url> <!-- #####Project Build ###--> <build> <!-- ###########给出构建过程中所用到的插件start######## --> <plugins> <!-- 由于maven默认使用的jdk与工程配置的jdk版本不一致,导致无法编译通过,通过该插件指定jdk版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- maven-surefire-plugin 是maven里执行测试用例的插件,不显示配置就会用默认配置。这个插件的 surefire:test 命令会默认绑定maven执行的 test 阶段 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 跳过测试单元 true:跳过测试,false不跳过(默认)--> <skip>true</skip> </configuration> </plugin> </plugins> <!-- ###########给出构建过程中所用到的插件end######## --> </build> <!-- ##########依赖属性参数配置 start############### --> <properties> <junit.version>4.11</junit.version> <cxf.version>2.2.3</cxf.version> <spring.version>3.2.3.RELEASE</spring.version> <slf4j.version>1.7.7</slf4j.version> </properties> <!-- ##########依赖属性参数配置 end############### --> <dependencies> <!-- 单元测试依赖包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!-- CXF Dependencies --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <!-- End of CXF Dependencies --> <!-- Spring Dependencies ${spring.version} --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </project>
6.配置wsdl用来发布WebService:beanRefServer_gd.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:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- Import Apache CXF Bean Definition --> <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"/> <!-- SurveyService --> <bean id="BankTransaction" class="gdsbcw_cxf.BankTransactionIMplementation"></bean> <!-- Expose SurveyWebService http://localhost:8080/sbcw_cxf/web_bank_transaction?wsdl--> <jaxws:server id="WebBankTransaction" serviceClass="gdsbcw_cxf.BankTransaction" address="/web_bank_transaction"> <jaxws:serviceBean> <ref bean="BankTransaction"/> <!-- 要暴露的 bean 的引用 --> </jaxws:serviceBean> </jaxws:server> </beans>
7.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>gdsbcw-cxf</display-name> <!-- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置 将参数名和参数值以键值对的形式加载到容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/beanRefServer_gd.xml</param-value> </context-param> <!-- 容器创建<listener>中的类实例,创建监听器。 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 用来声明一个servlet,CXFServlet是 --> <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>/*</url-pattern> </servlet-mapping> <!-- 会话超时配置(单位为分钟) --> <session-config> <session-timeout>60</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
8.服务器端布置检查:
8-1:入地址:http://localhost:8080/gdsbcw-cxf-01/
8-2:点击8-1中截图中的:WSDL : {http://gdsbcw_cxf/}BankTransactionService 或输入地址:http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction?wsdl
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="BankTransactionService" targetNamespace="http://gdsbcw_cxf/" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://gdsbcw_cxf/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types> <xs:schema elementFormDefault="unqualified" targetNamespace="http://gdsbcw_cxf/" version="1.0" xmlns:tns="http://gdsbcw_cxf/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="getAesSeed" type="tns:getAesSeed" /> <xs:element name="getAesSeedResponse" type="tns:getAesSeedResponse" /> <xs:element name="requestBank" type="tns:requestBank" /> <xs:element name="requestBankResponse" type="tns:requestBankResponse" /> <xs:element name="synAesSeed" type="tns:synAesSeed" /> <xs:element name="synAesSeedResponse" type="tns:synAesSeedResponse" /> <xs:complexType name="getAesSeed"> <xs:sequence> <xs:element minOccurs="0" name="sid" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="getAesSeedResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="requestBank"> <xs:sequence> <xs:element minOccurs="0" name="sid" type="xs:string" /> <xs:element minOccurs="0" name="xml" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="requestBankResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="synAesSeed"> <xs:sequence> <xs:element minOccurs="0" name="sid" type="xs:string" /> <xs:element minOccurs="0" name="seed" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="synAesSeedResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="requestBank"> <wsdl:part element="tns:requestBank" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="requestBankResponse"> <wsdl:part element="tns:requestBankResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="synAesSeed"> <wsdl:part element="tns:synAesSeed" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getAesSeed"> <wsdl:part element="tns:getAesSeed" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getAesSeedResponse"> <wsdl:part element="tns:getAesSeedResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="synAesSeedResponse"> <wsdl:part element="tns:synAesSeedResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="BankTransaction"> <wsdl:operation name="getAesSeed"> <wsdl:input message="tns:getAesSeed" name="getAesSeed"> </wsdl:input> <wsdl:output message="tns:getAesSeedResponse" name="getAesSeedResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="requestBank"> <wsdl:input message="tns:requestBank" name="requestBank"> </wsdl:input> <wsdl:output message="tns:requestBankResponse" name="requestBankResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="synAesSeed"> <wsdl:input message="tns:synAesSeed" name="synAesSeed"> </wsdl:input> <wsdl:output message="tns:synAesSeedResponse" name="synAesSeedResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="BankTransactionServiceSoapBinding" type="tns:BankTransaction"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="getAesSeed"> <soap:operation soapAction="" style="document" /> <wsdl:input name="getAesSeed"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="getAesSeedResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="requestBank"> <soap:operation soapAction="" style="document" /> <wsdl:input name="requestBank"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="requestBankResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="synAesSeed"> <soap:operation soapAction="" style="document" /> <wsdl:input name="synAesSeed"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="synAesSeedResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="BankTransactionService"> <wsdl:port binding="tns:BankTransactionServiceSoapBinding" name="BankTransactionPort"> <soap:address location="http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
9.配置客户端进行测试:
9-1:配置客户端访问服务器指定的services配置文件:spring_cxf-client.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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" /> <!-- serviceClass:要访问的接口,address:接口地址 -->
<!-- 注意:服务器配置为<jaxws:server />时,客户端地址不能带'?wsdl'
<jaxws:client id="BankTransactionClient" serviceClass="gdsbcw_cxf.BankTransaction" address="http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction?wsdl" /> -->
<jaxws:client id="BankTransactionClient" serviceClass="gdsbcw_cxf.BankTransaction" address="http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction" />
</beans>
9-2说明:address="http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction"
http://localhost:8080/gdsbcw-cxf-01:工程访问路径
/*:此路径在web.xml里面配置的,在gdsbcw-cxf-01工程名后面可以配置mapping的地址,/*表示任意访问
/web_bank_transaction:此路径在服务端的beanRefServer_gd.xml里配置的
spring_cxf-client.xml
10.编写客户端测试程序:TestBankTransaction
package gdsbcw_cxf; import gdsbcw_cxf.BankTransactionIMplementation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestBankTransaction { public static void main(String[] args) { //加载Spring配置文件 //ApplicationContext context = new ClassPathXmlApplicationContext("spring_cxf-client.xml"); //从Spring容器中获取对象 //BankTransaction bankTransaction = context.getBean("BankTransactionClient",BankTransaction.class); //远程调用接口 String a = bankTransaction.getAesSeed("a-"); String b = bankTransaction.synAesSeed("b-", "-b"); String c = bankTransaction.requestBank("c-", "<--xml格式>"); System.out.println(a); System.out.println(b); System.out.println(c); } }
运行结果:
11.通过soapUI工具测试:File->New soapUI Project(下载安装就略)
11-1命名工程名:gdsbcw_cxf(名字可以随便取);Initial WSDL/WADL:http://localhost:8080/gdsbcw-cxf-01/web_bank_transaction?wsdl
11-2:点击OK导入wsdl如下:可以看到服务器提供的3个接口以及一个接口测试的结果。