(五)CXF与spring的集成
使用代码优先开发webservice,cxf与spring的集成
?
服务端:将服务的发布交给spring完成
使用cargo插件在tomcat中独立发布项目
服务端提供一个cxf的webservice服务
pom.xml
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.hqh.cxf.ws</groupId><artifactId>cxf-web-server</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>cxf-web Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>3.0.5.RELEASE</spring.version><cxf.version>2.6.0</cxf.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><!-- cxf --><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></dependencies><build><pluginManagement><!-- 编译插件 --><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><!-- 编码 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>UTF-8</encoding></configuration></plugin><!-- cargo插件 配置tomcat --><plugin><groupId>org.codehaus.cargo</groupId><artifactId>cargo-maven2-plugin</artifactId><configuration><container><containerId>tomcat6x</containerId><home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home></container><configuration><type>standalone</type><home>${project.build.directory}/tomcat6x</home><properties><cargo.servlet.port>9999</cargo.servlet.port></properties></configuration><deployables> <deployable> <type>war</type> <properties> <context>/server</context> </properties> </deployable> </deployables></configuration></plugin></plugins></pluginManagement></build></project>?
?
接口
package com.hqh.cxf.ws;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebServicepublic interface IMyService {@WebResult(name="retString")public String getSomething(@WebParam(name="code")String code);}?
?
?
实现类
package com.hqh.cxf.ws;import javax.jws.WebService;@WebService(endpointInterface="com.hqh.cxf.ws.IMyService",serviceName="IMyservice")public class MyServiceImpl implements IMyService {@Overridepublic String getSomething(String code) {String reply = "";try {Integer.parseInt(code);reply = "success!"+code+"可以转换为int类型";} catch(NumberFormatException e) {reply = "fail!"+code+"不能转换为int类型";}return reply;}}?
?
创建cxf-servlet.xml放到WEB-INF目录下
<?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.xsdhttp://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-servlet.xml" /><bean id="cxfWsService" implementor="#cxfWsService" address="/cxf" /></beans>
?
?
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- spring配置文件地址 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 初始化beanFactory --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 集成cxf到spring --><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><init-param><param-name>config-location</param-name><param-value>/WEB-INF/cxf-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping></web-app>
?
注意:根据web.xml中【CXFServlet url-pattern】和cxf-servlet.xml【endpoint 的address】决定了spring发布的webservice的uri:/services/cxf
http://localhost:9999/server/services/cxf?wsdl
?
?
客户端:由spring完成webservice服务对象的注入
使用jetty插件启动客户端
客户端使用spring MVC创建Controller
pom.xml
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.hqh.cxf.ws</groupId><artifactId>cxf-web-client</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>cxf-web Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>3.0.5.RELEASE</spring.version><cxf.version>2.6.0</cxf.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><!-- cxf --><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></dependencies><build><finalName>cxf-web-client</finalName><plugins><!-- wsdl2java插件 --><plugin><groupId>org.apache.cxf</groupId><artifactId>cxf-codegen-plugin</artifactId><version>${cxf.version}</version><executions><execution><id>generate-sources</id><phase>compile</phase><configuration><sourceRoot>${project.build.directory}/generated/cxf</sourceRoot><wsdlOptions><wsdlOption><!-- 网络wsdl文件地址 --><wsdl>http://localhost:9999/server/services/cxf?wsdl</wsdl></wsdlOption></wsdlOptions></configuration><goals><goal>wsdl2java</goal></goals></execution></executions></plugin><!-- jetty插件 --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.10</version><configuration><scanIntervalSeconds>1000</scanIntervalSeconds></configuration></plugin></plugins></build></project>?
?
使用cxf-codegen-plugin的wsdl2java将网络wsdl文件转换为本地java文件
?
新建srpingMVC-servlet.xml【springMVC需要】
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <!-- 配置需要被扫描的包 --> <context:component-scan base-package="com.hqh.controller"/><!-- 配置对静态资源文件的访问不被过滤 --><mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 --> <bean value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>
?
?
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- spring配置文件地址 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 初始化beanFactory --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- springMVC --><servlet><servlet-name>srpingMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>srpingMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
?
?
WEB-INF下新建jsp目录,创建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form method="post">code:<input type="text" name="code"> <br/><input type="submit" value="提交"/></form><hr/>${retString}</body></html>?
?
创建Controller
package com.hqh.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.hqh.cxf.ws.IMyService;@Controllerpublic class HelloController {@Resourceprivate IMyService cxfWsService;@RequestMapping(value="/index",method=RequestMethod.GET)public String hello() {return "index";}@RequestMapping(value="/index",method=RequestMethod.POST)public String hello(String code, Model model) {System.out.println("code="+code);String retString = cxfWsService.getSomething(code);model.addAttribute("retString", retString);return "index";}}?
如何将webservice服务对象注入到controller中进行调用呢?
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 指定接口和服务地址,spring将自动管理该对象,然后就可以将IMyService注入到需要它的地方了 --> <jaxws:client id="cxfWsService" service/></beans>
?
?
?
?
?