使用CXF做webservice整合现有项目的例子

     从网上看了很多CXF的资料,大部分都是单独的作为一个webservice项目,对于在现有的spring项目上提供webservice服务的例子基本没有找到。

     我做的这个例子是介绍怎么把cxf整合到现有的spring项目中,现在只做到可以传简单的字符串和JAVABEAN,复杂的以后研究。

      这是例子的下载地址:一个简单的CXF例子

     一,应用cxf应该先把该服务所需要的架包加载进项目中。

             对于一个已经搭建好的spring项目,我做的项目中所缺少的架包是

              cxf-2.4.3.jar   ,   neethi-3.0.1.jar     ,     wsdl4j-1.6.2.jar     ,   xmlschema-core-2.0.1.jar    ,commons-logging-1.1.1.jar   ,spring一系列的架包

    二,首先是服务接口

package com.zcz.cxf.service;



import javax.jws.WebService;


@WebService 
public interface GreetingService { 
   public String greeting(String userName);
   public String say(String eat);
   //public String user(User user);
} 

      三,编写服务实现类

package com.zcz.cxf.service.impl;

import javax.jws.WebService;

import com.zcz.cxf.service.GreetingService;

@WebService
public class GreetingServiceImpl implements GreetingService { 

       public String greeting(String userName){
             return "你好! " + userName; 
       }

      public String say(String eat) {
	      return "该吃饭了"+eat;
	}

	
} 


 

       四,配置spring启动时,加载的xml文件,按照下面的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" />
	
	<jaxws:endpoint id="greetingService"
		implementor="com.gary.test.ws.service.impl.GreetingServiceImpl" 
		address="/GreetingService" />
</beans> 

       xmlns:jaxws=http://cxf.apache.org/jaxws

       http://cxf.apache.org/jaxwshttp://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" />

       <jaxws:endpoint id="greetingService"implementor="com.gary.test.ws.service.impl.GreetingServiceImpl" address="/GreetingService" />

       五,配置web.xml对于webservice的调用,在web.xml中添加以下代码即可

 

<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>/cxf/*</url-pattern>
	</servlet-mapping>
 


       六,启动项目如果访问http://localhost:8080/springmvcModel/cxf,出现如下图所示的内容则表示基于cxf的webservice配置成功



      七,客户端对于该接口的调用

              首先,新建一个与服务器端相同的服务接口类GreetingService

              其次,写调用服务的类

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


public class TestGreetingService {

	public static void main(String[] args) {
		//创建WebService客户端代理工厂  
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
		//注册WebService接口  
		factory.setServiceClass(GreetingService.class);  
		//设置WebService地址  
		factory.setAddress("http://localhost:8080/springmvcModel/cxf/GreetingService");  
		GreetingService greetingService = (GreetingService)factory.create();  
		System.out.println("开始调用webservice...");  
		System.out.println("返回的信息是:"+greetingService.say("米饭")); 

	}

}

      配置成功微笑,做完之后发现其实很简单,虽然不明白原理,但是会做基本的应用。


 

 

 

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值