`
yufenfei
  • 浏览: 797726 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CXF之一 (与Spring整合)

阅读更多

 

      WebService的主要目标是跨平台的可互操作性。为了达到这一目标,WebService完全基于XML(可扩展标记语言)、XSD(XMLSchema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。简单的说WebService就是一个应用程序,它向外界提供了一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web调用来实现某个功能的应用程序。

 

      CXF一个Apache开源的WebService框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。SOA(Service-Oriented Architecture)面向服务架构是一种思想,它将应用程序的不同功能单元通过中立的契约(独立于硬件平台、操作系统和编程语言联系起来,使得各种形式的功能单元更好的集成。目前来说,WebService 是SOA 的一种较好的实现方式,WebService 采用HTTP 作为传输协议,SOAP(Simple Object Access Protocol)作为传输消息的格式。

 

      在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息 格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。

 

熟悉了WebService的基础知识之后,入门CXF框架比较简单。CXF与Spring进行整合使用,大大的简化了开发工作。下面就拿HelloWorld来介绍CXF及WebService的基础性东西;

一、环境说明

 1、Spring的环境搭建,这里就不在说了。这里使用的spring2.5

 

 2、下载CXF,这里使用最新版本apache-cxf-2.6.2

 

 二 搭建CXF并与Spring进行整合

    1、所需的CXF的jar

 

    

    2、配置web.xml文件

 

 

 

         	<!--配置CXFServlet begin-->
			  	<servlet>
			  		<servlet-name>CXFServlet</servlet-name>
			  		<display-name>CXF Servlet</display-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可自定义配置,用于CXFServlet请求地址拦截,访问会用到 -->
			  		<url-pattern>/webservice/*</url-pattern>
			  	</servlet-mapping>
			  	<!--配置CXFServlet end -->
 

 

 

3、Web 服务的接口定义HelloWorld.java

 

 

	  @WebService(serviceName="HelloWorld",targetNamespace=Constants.WS_NAMESPACE)
			public interface HelloWorld {
			
				@WebMethod
				public String say(@WebParam(name="name")String name);
				
				@WebMethod
				public String sayHello(@WebParam(name="user")User user);
				
				@WebMethod
				public List<User> getList(Long id);
			}	  	
	

 

 

 

 

4、Web服务接口实现类 HelloWorldImpl.java

 

 

	                        @Service("HelloWorldImpl")
				public class HelloWorldImpl implements HelloWorld {
					public String say(String name) {
							return name+",您好!";
					}
				
					public String sayHello(User user) {
						return user.getName()+",您好!";
					}
					
					public List<User> getList(Long id){
						List<User> list = new ArrayList<User>();
						User user= new User();
						Long sid=1L;
						user.setId(sid);
						user.setName("张三"+sid);
						list.add(user);
						
						user= new User();
						sid=2L;
						user.setId(sid);
						user.setName("张三"+sid);
						list.add(user);
			
						return list;
					}
				}
 

注意:看到很多Web服务接口的实现类标注了 @WebService注解,我在这测试了实现类不标注该注解及@WebMethod正常。还没有看到具体文档

 

5、传递对象Customer的PO类定义

                       

 

 @XmlRootElement(name="User")
				@XmlAccessorType(XmlAccessType.FIELD)
				@XmlType(name="User")
				public class User{
					
					@XmlElement(nillable=true)
					private Long id;
					
					@XmlElement(nillable=true)
					private String name;
					
					@XmlElement(nillable=true)
					private int age;
				
					public Long getId() {
						return id;
					}
				
					public void setId(Long id) {
						this.id = id;
					}
				
					public String getName() {
						return name;
					}
				
					public void setName(String name) {
						this.name = name;
					}
				
					public int getAge() {
						return age;
					}
				
					public void setAge(int age) {
						this.age = age;
					}
				}					
		
 

 

 

这里使用很多注解,对于这些注解请参考 WebService注解详细 

 

 

6、CXF的Spring配置文件appCtx-cxf.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 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" />
				
				
				<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
				<bean id="outMessageInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
				
				<!--implementor:指定接口具体实现类,address:随意配-->
				<jaxws:endpoint id="helloWorld"  implementor="#HelloWorldImpl" address="/HelloWorld"  >
					<!-- 输入日志拦截器 -->
				   <jaxws:inInterceptors>
				   		<ref bean="inMessageInterceptor"/>
				   </jaxws:inInterceptors>
				   <!-- 输出日志拦截器 -->
				   <jaxws:outInterceptors>
				      <ref bean="outMessageInterceptor"/>
				   </jaxws:outInterceptors>
				</jaxws:endpoint>
				<!-- WebService 客户端 spring 配置文件cxf与Spring集成,cxf里提供了一个工厂类org.apache.cxf.jaxws.JaxWsProxyFactoryBean,
				可以方便实现的调用WebService。serviceClass属性是接口类,address是webService的路径在其他bean里如果要调用webservice,
				只要将client这个bean注入到需要使用的bean里。-->
				<bean id="client" class="com.exp.service.outer.HelloWorld" factory-bean="clientFactory" factory-method="create" />
				<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
					<property name="serviceClass" value="com.exp.service.outer.HelloWorld" />
					<property name="address" value="http://localhost:8080/demo/webservice/HelloWorld" />
				</bean>
			</beans>		
 

 

 

7、运行服务器

  访问http://localhost:8080/demo/webservice/HelloWorld?wsdl 地址,您会看到很长的XML文件

 

     看到的XML一大堆,不容易明白。具体解释参考 WSDL解释

  

8、客户端测试

    

 

 public class HelloWorldClient {
					public static void main(String[] args) {
						ApplicationContext context = new ClassPathXmlApplicationContext(
								new String[] { "classpath*:/config/spring_cfg/appCtx-*.xml" });
						HelloWorld client = (HelloWorld) context.getBean("client");
						System.out.println(client.say("ABC"));
						
						User user= new User();
						customer.setName("张三");
						System.out.println(client.sayHello(customer));
						
						List<User> list = client.getList(1L);
						for(User c:list){
							System.out.println(c.getName());
						}
					}
				}
 

 

 

     

 

  • 大小: 16.5 KB
分享到:
评论
4 楼 TopCombine 2015-05-28  
不错,测试通过
3 楼 Java_Worker_cr 2014-04-04  
3Q,楼主辛苦了
2 楼 wujiandao008 2013-12-03  
[b][u]
引用
[list]
[*][img][/img]
[/list]
[/u][/b]
1 楼 chenwill3 2013-09-03  
哥们,你用HTTPURLConnection试过了吗,貌似这样会出错。

相关推荐

Global site tag (gtag.js) - Google Analytics