spring3.0+cxf webservice开发

本文介绍了一个使用Spring和CXF实现WebService的具体案例。通过搭建基本环境、开发WebService接口及实现类,并配置相关文件,最终成功发布并测试了WebService。文中详细展示了配置文件及测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       对于webservice,之前只是有一点学习,算是一知半解吧。今天项目中需要做这么个功能,对外提供接口,我装作很懂webservice一样,说没问题,小事儿。其实心里是想等周末的时候自己查查资料,学习一下,因为这东西以前弄过,的确不难。谁知周末有事耽搁了,周一来了就要交差的。查了一会儿资料,突然想起项目中也许已经有了相关的例子呢,因为项目的平台早已搭建完了的。果然不出所料,于是就简单很多了。下面先将例子贴出来,然后讲解过程中遇到的小问题。

       一个简单的例子分为三步(基本环境、webservice类开发、配置文件)

       基本环境:  首先spring3.0的环境要搭好,cxf相关的jar引入进来。

       webservice类开发(接口和实现类):

package com.cxf.server;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import com.vo.WorkUnitVO;

@WebService
public interface IWorkUnitInfo {

	public List<WorkUnitVO> getWorkUnitByDate(Date date);
}
package com.cxf.server;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.service.IWorkUnitService;
import com..vo.WorkUnitVO;

@WebService(endpointInterface = "com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo")
public class WorkUnitInfoImpl implements IWorkUnitInfo {

	@Autowired
	private IWorkUnitService workUnitService;//项目中其他的类,获取结果集用到。 
	
	@Override
	public List<WorkUnitVO> getWorkUnitByDate(Date date) {
		return workUnitService.getWorkUnitByDate(date);
	}

	public IWorkUnitService getWorkUnitService() {
		return workUnitService;
	}

	public void setWorkUnitService(IWorkUnitService workUnitService) {
		this.workUnitService = workUnitService;
	}
	
}

配置文件1:(web.xml配置文件 加入cxf相关配置文件)

<!-- CXF Web Services 配置信息 -->
 <servlet>
  <display-name>CXF Servlet</display-name>
  <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>/services/*</url-pattern><!--注意这里的配置/services/ 这里是生成wsdl路径的一部分,wsdl的路径:http://ip:8080/项目路径/services/spring cxf配置文件中的address?wsdl-->
 </servlet-mapping>
配置文件2:(spring cxf 配置文件)

<?xml version="1.0" encoding="UTF-8"?>

<!-- START SNIPPET: beans -->
<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="helloWorld" 
	  implementor="com.cslc.crius.common.ws.cxf.server.HelloWorldImpl" 
	  address="/HelloWorld" />
	 
	<jaxws:endpoint
		id="workUnitInfo"
		implementor="#workUnitInfoImpl"
		address="/WorkUnitInfo"
	/>
	<bean id="workUnitInfoImpl" class="com.cslc.crius.common.ws.cxf.server.WorkUnitInfoImpl">

 <!--如果webservice中有需要加载其他类,采用注解的方式不行,需要在配置文件中显示声明-->  
		<property name="workUnitService" ref="workUnitServiceImpl"></property>
	</bean>
	  
</beans>
<!-- END SNIPPET: beans -->
以上几步完成以后既可以进行相关的测试了,发布到tomcat服务器中,启动服务器。
        第一步,测试wsdl:
访问 http://ip:8080/项目路径/services/spring cxf配置文件中的address?wsdl,例如:http://129.0.0.0:8080/abc/services/WorkUnitInfo?wsdl

第二步:测试webservice
       1、WebService 客户端 spring 配置文件 wsclient-context.xml  

<?xml version="1.0" encoding="UTF-8"?>

<!-- START SNIPPET: beans -->
<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/schema/jaxws.xsd">

	<bean id="workUnitInfoClient" class="com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo"
		factory-bean="workUnitInfoFactory" factory-method="create">
	</bean>
	
	<bean id="workUnitInfoFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo"/>
		<property name="address" value="http://localhost:8080/viminal/services/WorkUnitInfo?wsdl"></property>
	</bean>
	
</beans>
<!-- END SNIPPET: beans -->

2、调用WebService接口的测试程序 

package com.cxf.client;


import java.util.Date;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cslc.crius.common.utils.DateUtils;
import com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo;
import com.cslc.crius.viminal.vo.WorkUnitVO;

public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext(new String[] {"client/wsclient-context.xml"});

        IWorkUnitInfo work = (IWorkUnitInfo)context.getBean("workUnitInfoClient");
        Date date = DateUtils.parseDate("2012-10-01");
        List<WorkUnitVO> r = work.getWorkUnitByDate(date);
        for (int i=0;i<r.size();i++) {
        	System.out.println(r.get(i).getWorkUnitTitle());
        }
        System.exit(0);
        // END SNIPPET: client
    }
}
基本步骤就是上面这些,对外提供的接口就是上面测试wsdl所用的url。

<pre>









                
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值