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

Spring+CXF开发WebService

阅读更多

准备工作:

下载spring,apache-cxf,soapUI

 

新建Web项目

 

接口

 

package com.gary.test.ws.service;

import javax.jws.WebService;

@WebService 
public interface GreetingService { 
   public String greeting(String userName); 
} 

 

 实现

 

package com.gary.test.ws.service.impl;

import java.util.Calendar;

import javax.jws.WebService;

import com.gary.test.ws.service.GreetingService;

@WebService(endpointInterface = "com.gary.test.ws.service.GreetingService") 
public class GreetingServiceImpl implements GreetingService { 

   public String greeting(String userName){
       return "Hello " + userName + ", currentTime is " + Calendar.getInstance().getTime(); 
   } 
} 

 

 applicationContext.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> 
 

 

web.xml如下

 

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<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>/*</url-pattern>
	</servlet-mapping>
</web-app>
 

 

到此代码部分结束,所需jar包列表


 

部署到tomcat,项目启动后可以看到


安装soapUI,打开soapUI,new soapUI Project,如下,WSDL写之前配置的地址http://gary-pc:8080/testWebService/GreetingService?wsdl ,OK


 

添加参数arg0的值,submit,可以看到右侧的返回结果


 

OK测试通过,成功返回正确结果

 

不用soapUI的话也可以直接写测试类

package com.gary.test.ws.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.gary.test.ws.service.GreetingService;

public class TestGreetingService {
	public static void main(String[] args) {
		//创建WebService客户端代理工厂
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		//注册WebService接口
		factory.setServiceClass(GreetingService.class);
		//设置WebService地址
		factory.setAddress("http://gary-pc:8080/testWebService/GreetingService");
		GreetingService greetingService = (GreetingService)factory.create();
		System.out.println("invoke webservice...");
		System.out.println("message context is:"+greetingService.greeting("gary"));   
	}
}
 

 

 

  • 大小: 32.9 KB
  • 大小: 66.2 KB
  • 大小: 70.8 KB
  • 大小: 194.7 KB
11
5
分享到:
评论
15 楼 djb_daydayup 2015-11-27  
很好,受用了!
13 楼 java_qgsmn 2015-06-25  
为啥例子报faultCode=INVALID_WSDL: : The declaration for the entity "HTML.Version" must end with '>'.这个错   在eclipse中的web service explorer中
12 楼 guoleixi001 2015-04-09  
chuyuan_china 写道

随便用哪个IDE都可以的~工具不一样 实现方式一样 我就是用的 MyEclipse
11 楼 rvk20025 2015-01-20  
很好用~~~
10 楼 张黎明 2014-05-29  
缺少jar包:jaxb-api-2.2.jar;jaxws-api-2.2.8.jar;
9 楼 abc562311934 2014-05-16  
java_stream 写道
为什么你这个例子会报错?警告: Exception thrown from ApplicationListener handling ContextClosedEvent
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'cxf': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:209)

我的也报错了。。。。
8 楼 cyheye 2013-12-25  
谢谢。终于找到了个能正常运行的例子。
对初学者入门很有帮助。
7 楼 java_stream 2013-11-05  
为什么你这个例子会报错?警告: Exception thrown from ApplicationListener handling ContextClosedEvent
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'cxf': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:209)
6 楼 tuposky 2013-11-01  
soapui真好用,哈哈。
5 楼 sunlibao123 2013-03-14  
例子很简单,但很说明问题
4 楼 things07 2012-09-21  

但不知楼主用的是哪个IDE??
3 楼 chuyuan_china 2012-08-27  
2 楼 tianyaclubclubtianya 2012-08-21  
很详细    xml webService 与webService 的区别
1 楼 zhufeng1981 2011-10-30  
不错,SoapUI能否讲些复杂例子。

相关推荐

    spring+cxf 开发webservice

    【标题】"Spring+CXF 开发Web Service" 在Java世界中,开发Web服务的一个常见选择是使用Spring框架结合Apache CXF。Spring作为一个强大的轻量级框架,提供了丰富的功能,包括依赖注入、AOP(面向切面编程)以及企业...

    Spring+CXF开发WebService源代码

    本教程将基于"Spring+CXF开发WebService源代码"的主题,深入探讨如何利用这两者创建和消费Web服务。 首先,Spring是一个开源的应用框架,提供了一个全面的编程和配置模型,适用于Java应用程序。它为开发企业级应用...

    Spring+CXF+tomcat开发webservice

    这个项目"Spring+CXF+tomcat开发webservice"旨在教你如何利用这些技术搭建一个完整的Web服务环境,包括服务端和服务端客户端的实现。 **Spring** 是一个广泛使用的Java企业级应用开发框架,它提供了依赖注入(DI)...

    使用Eclipse+Maven+Spring+CXF构建的WebService服务

    Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/

    Spring+cxf请求webService

    在Java开发领域,Spring框架以其强大的依赖注入和面向切面编程能力被广泛应用,而CXF则是一个优秀的开源服务开发框架,支持SOAP和RESTful服务。当Spring与CXF结合使用时,可以方便地创建和消费Web服务。本篇文章将...

    Spring + cxf = webservice 完整实例源码免费下载

    Spring + cxf = webservice 完整实例源码免费下载 完全免费。此资源仅为文档提供。 版权为百度文档 "Spring + cxf = webservice 完整实例源码免费下载" 所有。

    spring+cxf的webservice

    标题中的“spring+cxf的webservice”指的是使用Spring框架与Apache CXF库共同构建Web服务的过程。Apache CXF是一个开源的Java框架,主要用于创建和消费Web服务,它支持SOAP、RESTful等多种通信协议。Spring框架则是...

    Spring+cxf=webservice 的jar包1

    【Spring+cxf=WebService】是将Spring框架与Apache CXF集成来实现Web服务的一种常见方式。Spring是一个强大的Java企业级应用开发框架,它提供了一种模块化和灵活的方式来组织和管理应用程序的组件。而CXF则是一个...

    mybatis+spring+cxf Webservice框架

    【标题】"mybatis+spring+cxf Webservice框架"是一个集成性的开发框架,它结合了三个主流的技术组件:MyBatis、Spring和Apache CXF,用于构建高效、灵活且易于维护的Web服务。MyBatis是一个优秀的持久层框架,Spring...

    使用Spring+CXF开发WebService.doc

    在使用Spring和Apache CXF开发Web服务时,我们可以利用CXF提供的强大功能,与Spring框架进行无缝集成。CXF允许开发者通过注解和Spring配置文件来轻松地暴露和消费Web服务。以下是关于如何使用这些技术的一些关键知识...

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验 ...通过本实验,我们可以学习到Spring Boot的自动配置和依赖管理功能、Mybatis的数据库操作功能和CXF的WebService开发功能。

    ibatis+spring+cxf+mysql搭建webservice的客户端

    ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729

    spring+CXF实现WebService(http+https)

    Spring框架是Java企业级应用开发的首选,而CXF是一个强大的开源服务框架,支持创建和消费Web服务。本教程将深入探讨如何利用Spring和CXF来实现基于HTTP和HTTPS的Web服务,并且将涉及到HTTPS的证书配置。 1. **...

    Spring+CXF 发布WebService服务

    在IT行业中,Spring框架和Apache CXF是两个非常重要的组件,它们在开发Web服务时扮演着关键角色。本文将深入探讨如何使用Spring和CXF来发布WebService服务。 首先,Spring是一个开源的Java平台,它提供了全面的编程...

    maven+spring+cxf webservice demo

    【描述】"maven+spring+cxf 的webservice开发源码,欢迎大家指点,谢谢"表明这个项目是一个开源的学习资源,包含了实际的源代码,开发者可以借此学习如何在实际项目中使用Maven、Spring和CXF来开发Web服务。...

    Webservice笔记含使用cxf和jaxws两种方式开发webservice【源代码+笔记】

    第一天: 什么是webservice? 从案例(便民查询网站)分析如何实现?...CXF开发webservice: CXF入门程序 Spring+cxf整合(重点) CXF发布rest的webservice。(重点) 综合案例: 实现便民查询网站

    源码-springboot+cxf实现webservice服务端

    ### 源码分析:Spring Boot + CXF 实现WebService服务端 #### 一、概述 随着企业级应用之间的交互需求日益增长,跨平台、跨语言的服务调用变得尤为重要。WebService作为一种成熟且广泛采用的技术标准,能够很好地...

    spring+mybatis+cxf webservice实现

    调用webservice,插入数据,整合druid监控数据源;... webservice配置文件在spring-mvc.xml和web.xml中; 数据库监控地址http://localhost:8080/taixingMsg/druid/index.html... 各位同仁,生命不止,奋斗不息

    Spring+cxf=webservice 的jar包2

    标题中的“Spring+cxf=webservice 的jar包2”表明这是一个与Spring框架和Apache CXF相关的Web服务实现项目。在Java开发中,Spring是一个广泛使用的轻量级框架,用于简化企业级应用的开发,而CXF则是一个开源的服务栈...

Global site tag (gtag.js) - Google Analytics