cxf + spring 开发基于web服务的分布式异构数据同步更新应用技术研究

本文介绍使用CXF 2.6.1、Spring 3.1.0 和 Hibernate 3.6搭建Web服务的过程,包括服务端接口定义、实现及Spring配置等关键步骤。

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

本课题主要解决不同平台和不同应用系统之间的数据转换、数据同步和多个数据源的共享集成问题。为解决该问题,采用基于Web服务的方式来进行数据的交互,主要要考虑如何保持两个系统中的数据能实时同步更新(增加、删除和修改)。

用cxf-2.6.1+spring 3.1.0+ hibernate3.6 版本开发web services

 

服务端的开发

interface IHelloService

package com.dx.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.dx.model.User;

@WebService
public interface IHelloService {

	@WebMethod
	public String sayHello(String username);
	
	@WebMethod
	public User getUser(int id);
	
	@WebMethod
	public void add(User user);
}

 

IHelloService 的实现 IHelloServiceImpl

package com.dx.service.impl;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.dx.dao.BaseDao;
import com.dx.model.User;
import com.dx.service.IHelloService;

@WebService
public class IHelloServiceImpl implements IHelloService{

	private BaseDao baseDao;
	
	
	@Override
	@WebMethod
	public String sayHello(String username) {
		return "Hello "+username;
	}

	@Override
	@WebMethod
	public User getUser(int id) {
		User user = new User();
		user.setName("杨钰莹");
		user.setId(id);
		
		return user;
	}

	@Override
	@WebMethod
	public void add(User user) {
		baseDao.save(user);
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
}

 

spring applicationContext-user.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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://cxf.apache.org/jaxws  
	http://cxf.apache.org/schemas/jaxws.xsd">

	<jaxws:endpoint id="hellos"  address="/Hellows" implementor="#helloService">
	</jaxws:endpoint>
	
	<bean id="helloService" class="com.dx.service.impl.IHelloServiceImpl">
	  <property name="baseDao" ref="baseDao"></property>
	</bean>
	
	
	
	
</beans>

 web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>cxf</display-name>
    
    
    
    <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
    classpath:context/applicationContext-*.xml
 </param-value>
 </context-param>

 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
    
    

    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

 

好了,接下来部署启动tomcat ,在浏览器输入 http://localhost:8080/cxf_web_010/services

 

在出现的页面上 点击WSDL : {http://impl.service.dx.com/}IHelloServiceImplService

出现页面

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="IHelloServiceImplService" targetNamespace="http://impl.service.dx.com/" xmlns:ns1="http://service.dx.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.dx.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:import location="http://localhost:8080/cxf_web_010/services/Hellows?wsdl=IHelloService.wsdl" namespace="http://service.dx.com/" /> 
- <wsdl:binding name="IHelloServiceImplServiceSoapBinding" type="ns1:IHelloService">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <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:operation name="add">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="add">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="addResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <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:binding>
- <wsdl:service name="IHelloServiceImplService">
- <wsdl:port binding="tns:IHelloServiceImplServiceSoapBinding" name="IHelloServiceImplPort">
  <soap:address location="http://localhost:8080/cxf_web_010/services/Hellows" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

现在web services 已经发布了,下一章编写客户端

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值