`

Spring中使用CXF java的webservice

    博客分类:
  • java
阅读更多
Spring中使用CXF
关键字: cxf spring webservice
在Spring中采用CXF来使用WebService是很方便的,这是按照Apache官方网站上的文章写的。
1.Web服务接口HelloWorld.java:
Java代码
1. package demo.spring; 
2.  
3. import javax.jws.WebService; 
4.  
5. @WebService 
6. public interface HelloWorld { 
7.     String sayHi(String text); 
8. } 
2.实现类HelloWorldImpl.java:
Java代码
1. package demo.spring; 
2.  
3. import javax.jws.WebService; 
4.  
5. @WebService(endpointInterface = "demo.spring.HelloWorld") 
6. public class HelloWorldImpl implements HelloWorld { 
7.  
8.     public String sayHi(String text) { 
9.         return "Hello " + text; 
10.     } 
11. } 
3.Spring配置文件beans.xml:
Xml代码
1. <beans xmlns="http://www.springframework.org/schema/beans" 
2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3.     xmlns:jaxws="http://cxf.apache.org/jaxws" 
4.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
5.     http://www.springframework.org/schema/beans/spring-beans.xsd 
6.     http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> 
7.  
8.     <import resource="classpath:META-INF/cxf/cxf.xml" /> 
9.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
10.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
11.  
12.     <jaxws:endpoint  
13.       id="helloWorld"  
14.       implementor="demo.spring.HelloWorldImpl"  
15.       address="/HelloWorld" /> 
16.        
17. </beans> 
4.在web.xml文件中加入:
Xml代码
1. <context-param> 
2.     <param-name>contextConfigLocation</param-name> 
3.     <param-value>WEB-INF/beans.xml</param-value> 
4. </context-param> 
5.  
6. <listener> 
7.     <listener-class> 
8.         org.springframework.web.context.ContextLoaderListener 
9.     </listener-class> 
10. </listener> 
11.  
12. <servlet> 
13.     <servlet-name>CXFServlet</servlet-name> 
14.     <servlet-class> 
15.         org.apache.cxf.transport.servlet.CXFServlet 
16.     </servlet-class> 
17.     <load-on-startup>1</load-on-startup> 
18. </servlet> 
19.  
20. <servlet-mapping> 
21.     <servlet-name>CXFServlet</servlet-name> 
22.     <url-pattern>/*</url-pattern> 
23. </servlet-mapping> 
5.客户端调用时Client.java:
Java代码
1. package demo.spring.client; 
2.  
3. import demo.spring.HelloWorld; 
4.  
5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
6.  
7.  
8. public final class Client { 
9.  
10.     private Client() { 
11.     } 
12.  
13.     public static void main(String args[]) throws Exception { 
14.         ClassPathXmlApplicationContext context  
15.             = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"}); 
16.  
17.         HelloWorld client = (HelloWorld)context.getBean("helloWorld"); 
18.  
19.         String response = client.sayHi("Joe"); 
20.         System.out.println("Response: " + response); 
21.     } 
22. } 
  client-beans.xml
Xml代码
1. <beans xmlns="http://www.springframework.org/schema/beans" 
2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3.     xmlns:jaxws="http://cxf.apache.org/jaxws" 
4.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
5.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
6.     http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> 
7.      
8.     <bean id="helloWorld" class="demo.spring.HelloWorld"  
9.       factory-bean="clientFactory" factory-method="create"/> 
10.      
11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
12.       <property name="serviceClass" value="demo.spring.HelloWorld"/> 
13.       <property name="address" value="http://localhost:8080/cxf2/HelloWorld"/> 
14.     </bean> 
15.        
16. </beans> 
分享到:
评论

相关推荐

    spring+CXF实现WebService(http+https)

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

    Spring2+CXF实现webservice笔记

    Spring 框架与 Apache CXF 结合使用可以极大简化 WebService 的开发过程。本文将基于提供的文件信息,深入探讨如何利用 Spring2 和 CXF 轻松地实现 WebService 接口,并详细解析相关的注解及其作用。 #### 二、...

    使用CXF和camel-cxf调用webservice

    你可以使用CXF的工具,如wsdl2java,将这个WSDL文件转换为Java客户端代码,然后在项目中引用这些代码来调用Web服务。 总的来说,Apache CXF和camel-cxf提供了强大的工具集,帮助开发者高效地集成和管理Web服务。...

    Spring+CXF 发布WebService服务

    本文将深入探讨如何使用Spring和CXF来发布WebService服务。 首先,Spring是一个开源的Java平台,它提供了全面的编程和配置模型,用于简化企业级应用的开发。Spring框架的核心特性包括依赖注入、面向切面编程(AOP)...

    基于spring注解的cxf实现webservice

    总结,基于Spring注解的CXF实现Web服务,主要涉及Spring注解的使用、服务接口的定义、服务实现的创建、CXF与Spring的配置以及部署和测试过程。这种方式极大地简化了Web服务的开发,使得代码更加清晰,维护更方便。...

    使用CXF发布WebService

    总的来说,这个项目展示了如何使用Java技术栈(Spring、MyBatis、CXF)以及Oracle数据库来创建一个完整的Web服务解决方案。通过CXF发布服务,其他系统(如使用Axis的客户端)可以跨网络调用这些服务,实现不同应用间...

    Apache CXF2+Spring2.5轻松实现WebService

    本教程将深入探讨如何利用Apache CXF 2与Spring 2.5来构建和使用WebService。 首先,让我们理解这两个组件的基本概念。Apache CXF是一个全面的服务框架,它支持多种Web服务规范,如SOAP、RESTful、WS-*等。它提供了...

    spring+cxf 开发webservice

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

    maven - spring4.1.6和cxf3.0.8 WebService整合代码

    这个示例中的具体应用是通过一个名为"HelloWorld"的服务来展示的,其Web Service地址为`http://localhost:8080/springCxf/HelloWorld?wsdl`。让我们逐步了解这个过程。 **1. Spring框架介绍** Spring是一个广泛使用...

    CXF2.7+Spring3 Java WebService 集成用例

    在本文中,我们将深入探讨如何将Apache CXF 2.7与Spring 3框架集成,以便在Java环境中创建和消费Web服务。这是一个重要的技术组合,因为它允许开发人员利用Spring的依赖注入和管理能力,以及CXF的强大Web服务支持。...

    spring+cxf的webservice

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

    SpringBoot框架及CXF发布WebService

    3. 使用CXF的注解或Java配置定义Web服务接口和实现。 4. 创建并运行SpringBoot应用,CXF将会自动启动并暴露Web服务。 在给定的压缩包文件中,"WebService_Server"可能包含了SpringBoot与CXF集成的服务器端代码示例...

    Spring+cxf请求webService

    在Spring+CXF的环境中,XML请求通常是通过Java对象转换得到的。CXF提供了JAXB(Java Architecture for XML Binding)支持,可以将Java对象自动转换为XML,反之亦然。因此,调用Web服务方法时,传入Java对象,CXF会...

    使用CXF开发WebService

    本篇内容将深入探讨如何使用CXF和Spring来创建和使用Web服务。 首先,我们需要了解CXF中的关键注解: 1. **@WebService**:此注解用于标记一个接口或者实现类为Web服务。`name`、`serviceName`和`targetNamespace`...

    spring整合CXF开发webService接口所需的全部jar包

    - **服务发布**:使用Spring的`@WebService`注解和`JaxWsServerFactoryBean`,可以在Spring容器中发布Web服务。 - **服务消费**:利用`JaxWsProxyFactoryBean`,可以方便地在Spring中创建Web服务客户端。 4. **...

    运用spring和CXF开发webservice

    3. **配置Spring**:在Spring的配置文件中,定义一个Bean来表示我们的服务实现类,并使用`jaxws:endpoint`标签声明CXF的Web服务端点。这样,Spring会自动处理服务的生命周期和依赖注入。 4. **配置CXF**:配置CXF的...

    springboot(5) 整合cxf提供webservice服务

    在本教程中,我们将深入探讨如何使用Spring Boot与Apache CXF整合来创建并提供Web服务。Spring Boot以其简化Java应用程序开发的特性而受到广泛欢迎,而CXF则是一个流行的开源框架,用于构建和消费Web服务。当我们...

    基于spring3.0的cxf发布webservice+client的

    Spring框架以其灵活性和模块化特性在Java开发中广泛应用,而CXF是Spring支持的一个开源服务堆栈,用于构建和部署Web服务。本篇文章将围绕“基于Spring 3.0的CXF发布Web Service及客户端”的主题展开,详细介绍如何...

    使用cxf 开发webService 初体验

    3. **配置CXF**:在Spring配置文件中配置CXF的Servlet或Jetty服务器,以便托管Web Service。 **五、部署Web Service** 1. **打包服务**:将生成的Java类和配置文件打包成WAR或JAR文件。 2. **部署服务**:将打包好...

    spring整合cxf 实现webservice

    【Spring 整合 CXF 实现 WebService】是将 Apache CXF 框架与 Spring 框架结合,以创建、部署和管理 WebService 的一种方法。Apache CXF 是一个开源服务框架,它允许开发者创建和消费各种 Web 服务,而 Spring 提供...

Global site tag (gtag.js) - Google Analytics