Spring CXF实例
使用apache CXF和maven开发Web Service http://www.cnblogs.com/holbrook/archive/2012/12/12/2814821.html
Web Service的CXF实现(Spring整合方式) http://m.oschina.net/blog/196308
一个依赖问题:
tomcat下运行cxf异常 java.lang.ClassCastException http://m.blog.csdn.net/blog/xyzroundo/9086205
A: 如果希望以一种一致的方式实现webservice,特别是有跨语言的需求时,应该使用Axis2
B: 如果需要在现有的java程序(包括web应用)中增加webservice支持,应该使用CXF
注解:
标注 说明
WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
WebMethod 定制Web Service方法
WebParam 定制Web Service方法的参数
WebResult 定制Web Service方法的返回值
SOAPBinding 指定WebService的SOAP映射样式
pom.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
Xml代码 <?xml version="1.0" encoding="UTF-8"?> <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>SpringCXF</groupId> <artifactId>SpringCXF</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.8.RELEASE</spring.version> <cxf.version>2.4.1</cxf.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</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-context-support</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- CXF Dependencies --> <dependency> <groupId>jta</groupId> <artifactId>jta</artifactId> <version>1.0.1b</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>${cxf.version}</version> </dependency> <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-databinding-aegis</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-local</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> <exclusions> <exclusion> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-jms</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-management</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-common-utilities</artifactId> <version>${cxf.version}</version> </dependency> <!-- End of CXF Dependencies --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <username>admin</username> <password>admin</password> <path>/SpringCXF</path> </configuration> </plugin> </plugins> </build> </project> |
web.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
Xml代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0"> <display-name>Spring3MVC</display-name> <!-- 配置SpringMVC的applicationContext.xml文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置SpringMVC --> <servlet> <servlet-name>spring-mvc-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc-servlet</servlet-name> <url-pattern>/web/*</url-pattern> </servlet-mapping> <!-- 配置SpringWS --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <!--<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
spring-mvc-servlet.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Xml代码 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <mvc:annotation-driven/> <mvc:resources location="/resources/" mapping="/resources/**"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> |
applicationContext.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-autowire="byName" default-lazy-init="true"> <context:component-scan base-package="com"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> <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" /> <!-- 定义一个发布点,和发布点的地址 --> <jaxws:endpoint id="helloWorld" implementor="com.service.impl.HelloWorldImpl" address="/HelloWorld" /> </beans> |
User.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
Java代码 package com.module; import java.io.Serializable; /** * Created by Administrator on 14-5-21. */ public class User implements Serializable { private static final long serialVersionUID = 2035807023987192387L; private String userName; private String userCode; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof User)) return false; User user = (User) o; if (userCode != null ? !userCode.equals(user.userCode) : user.userCode != null) return false; if (userName != null ? !userName.equals(user.userName) : user.userName != null) return false; return true; } @Override public int hashCode() { int result = userName != null ? userName.hashCode() : 0; result = 31 * result + (userCode != null ? userCode.hashCode() : 0); return result; } @Override public String toString() { return "User{" + "userName='" + userName + '\'' + ", userCode='" + userCode + '\'' + '}'; } } |
下面开始写服务器端代码,首先定制服务器端的接口,代码如下:
IHelloWorld.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Java代码 package com.service; import javax.jws.WebService; /** * Created by Administrator on 14-5-21. * 定制服务器端的接口 */ @WebService public interface IHelloWorld { public String sayHello(String name); public User getUser(String name); } |
下面编写WebService的实现类,服务器端实现代码如下:
注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
HelloWorldImpl.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Java代码 package com.service.impl; import com.service.IHelloWorld; /** * Created by Administrator on 14-5-21. * WebService的实现类,服务器端实现代码 * 注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。 */ public class HelloWorldImpl implements IHelloWorld { public String sayHello(String name) { System.out.println("sayHello is called by " + name); return "服务器返回的信息[" + name+"]"; } @Override public User getUser(String name) { User user = new User(); user.setUserCode("Pandy"); user.setUserName(name); return user; } } |
下面要在applicationContext-server.xml文件中添加如下配置: applicationContext-client.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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"/> <!-- 定义一个客户端的类,用来连接WebService的信息 这个和服务端开放的借口是实现同一个类的。 --> <jaxws:client id="helloWorldClient" address="http://localhost:8080/SpringCXF/ws/HelloWorld" serviceClass="com.service.IHelloWorld"/> </beans> |
下面启动tomcat服务器后,在WebBrowser中请求:
http://localhost:8080/SpringCXF/ws/HelloWorld?wsdl
———————-
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Xml代码 <?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="HelloWorldImplService" targetNamespace="http://impl.service.com/" xmlns:ns1="http://service.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:import location="http://localhost:8080/SpringCXF/ws/HelloWorld?wsdl=IHelloWorld.wsdl" namespace="http://service.com/"> </wsdl:import> <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:IHelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getUser"> <soap:operation soapAction="" style="document"/> <wsdl:input name="getUser"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="getUserResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="sayHello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="sayHelloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldImplService"> <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://localhost:8080/SpringCXF/ws/HelloWorld"/> </wsdl:port> </wsdl:service> </wsdl:definitions> |
如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的HelloWorld就是上面xml配置中的address的名称,是一一对应的。
下面编写客户端请求的代码,代码如下:
Client.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Java代码 package com.client; /** * Created by Administrator on 14-5-21. * 客户端调用服务端,许多东西都被Spring和CXF自己处理了,只实现调用和输出就好。 * */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.IHelloWorld; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml"); IHelloWorld helloWorld = (IHelloWorld) context.getBean("helloWorldClient"); System.out.println(helloWorld.sayHello("客户端传进去的信息")); System.out.println(helloWorld.getUser("Panyongzheng").toString()); } } |
先运行服务器
在以应用程序的方式运行客户端,得到输出:
……
org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.com/}IHelloWorldService from class com.service.IHelloWorld
服务器返回的信息[客户端传进去的信息]
