`
lxy19791111
  • 浏览: 479490 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

Spring中使用CXF

阅读更多

在Spring中采用CXF来使用WebService是很方便的,这是按照Apache官方网站上的文章写的。

 1.Web服务接口HelloWorld.java:

package demo.spring;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}

 2.实现类HelloWorldImpl.java:

package demo.spring;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        return "Hello " + text;
    }
}

 3.Spring配置文件beans.xml:

<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">

	<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="demo.spring.HelloWorldImpl" 
	  address="/HelloWorld" />
	  
</beans>

 4.在web.xml文件中加入:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>WEB-INF/beans.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>

 5.客户端调用时Client.java:

package demo.spring.client;

import demo.spring.HelloWorld;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});

        HelloWorld client = (HelloWorld)context.getBean("helloWorld");

        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);
    }
}

  client-beans.xml

<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-2.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
	
    <bean id="helloWorld" class="demo.spring.HelloWorld" 
      factory-bean="clientFactory" factory-method="create"/>
    
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	  <property name="serviceClass" value="demo.spring.HelloWorld"/>
	  <property name="address" value="http://localhost:8080/cxf2/HelloWorld"/>
	</bean>
	  
</beans>

 源代码详见本文附件。

  • cxf2.rar (2.5 KB)
  • 描述: 此文的源代码
  • 下载次数: 1338
分享到:
评论
8 楼 adversewind 2014-06-30  
7 楼 uule 2012-11-21  
客户端也可以这么用:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(clazz);
factory.setAddress(wsAddress);
client = factory.create();
6 楼 lzl8910 2012-07-31  
[color=red][/color]


怎么少包了?
5 楼 tiantiantianti 2012-06-07  
学习学习很好的入门资料
4 楼 chxiaowu 2011-11-09  
<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" /> 


这几个文件从那里来啊?
3 楼 zmgsxit 2011-01-13  
请教报错原因??

xml文件如下 <bean id="identityValidateServiceClient" class="com.sxit.webservices.WebServiceSample"
factory-bean="identityValidateServiceClientFactory" factory-method="create" />

<bean id="identityValidateServiceClientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="com.sxit.webservices.WebServiceSample" />
<property name="address"
value="http://127.0.0.1/hainan/services/HelloWorld"/>
</bean>


org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:244)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
at $Proxy44.say(Unknown Source)
at com.sxit.webservices.Client.main(Client.java:27)
Caused by: java.io.IOException: IOException invoking http://127.0.0.1/hainan/services/HelloWorld: HTTP response '404: Not Found'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAc
2 楼 lxy19791111 2010-04-15  
aguai0 写道
怎么少了包啊

不好意思,因为包比较大,所以没有上传,不过你可以到官网下载。
1 楼 aguai0 2010-04-14  
怎么少了包啊

相关推荐

    spring 3.0.5 集成cxf

    在IT行业中,Spring框架是Java企业级应用开发的首选,而CXF则是一个流行的开源服务框架,用于构建和消费Web服务。"spring 3.0.5 集成cxf"这一主题聚焦于如何将Spring 3.0.5版本与Apache CXF整合,以实现高效、灵活的...

    Spring4.3整合CXF3.0.4需要使用的Jar包--亲测可用

    Spring4.3整合CXF3.0.4需要使用的Jar包,Spring4.0以上需要使用CXF3.0以上的jar包才可以,之前使用CXF2.7会报各种错误,cxf-core-3.0.4.jar,cxf-rt-bindings-soap-3.0.4.jar

    spring4+cxf3

    在IT行业中,Spring框架和Apache CXF是两个非常重要的组件,它们在企业级应用开发中起着关键作用。本文将详细探讨"spring4+cxf3"整合的相关知识点,包括Spring 4.1.8和CXF 3.1.3的特性、整合过程以及实际应用场景。 ...

    spring+cxf 整合jar包

    1. **服务提供者(Service Provider)**:在Spring+CXF整合中,我们可以使用Spring配置来定义CXF服务。这包括服务接口、实现类、服务终结点(Endpoint)等,通过XML配置文件或注解进行声明。 2. **服务消费者...

    Spring3整合CXF2.7.10

    - **异常处理**: 使用CXF的异常处理器和Spring的AOP来统一处理服务调用中的异常。 - **消息转换**: Spring与CXF结合,可以方便地使用各种消息格式,如XML、JSON等。 - **服务版本控制**: 实现服务版本管理,以适应...

    cxf整合spring

    在IT行业中,Web服务是应用程序之间进行通信的一种标准方法,而CXF和Spring都是Java生态系统中的关键组件。本文将深入探讨如何将CXF与Spring框架整合,以构建高效且灵活的Web服务解决方案。 首先,让我们了解CXF。...

    Spring整合CXF demo

    这个"Spring整合CXF demo"项目提供了一个直观的例子,帮助开发者理解如何在Spring环境中配置和使用CXF服务。 ### 1. Spring框架介绍 Spring是一个开源的Java平台,它提供了全面的应用程序开发框架,简化了Java EE...

    cxf+spring使用经验

    【cxf+spring 使用经验】 Apache CXF 是一个开源的 Web 服务框架,它整合了 Celtix 和 XFire 两大项目的优势,提供了全面的 JAX-WS 支持,允许开发者通过 Code First 或 WSDL First 的方式来创建和消费 Web 服务。...

    利用spring security 给cxf的业务方法添加保护

    在IT行业中,安全是任何应用程序的核心组成部分,Spring Security作为一个强大的安全框架,被广泛用于保护Java应用,包括基于CXF的服务。本篇文章将深入探讨如何利用Spring Security为CXF的业务方法添加保护,确保...

    spring3+cxf2.7 整合jar包

    这样的整合使得开发者能够在SpringMVC环境中方便地使用CXF来实现服务的发布和调用,提高应用的灵活性和可扩展性。 描述中提到的"springMVC整合cxf所需的jar包",意味着这个压缩包中包含了使得SpringMVC能够与CXF...

    cxf2.7+spring4

    在Spring 4中,我们还可以利用注解驱动的方式来简化配置,例如使用`@WebService`注解标记服务接口,`@WebServiceService`注解标记服务实现,然后在Spring配置中使用`@ImportResource`导入CXF的配置。 此外,对于...

    Spring CXF Restful 实例

    通过这个“Spring CXF Restful实例”,开发者将学习到如何在Spring环境中使用CXF构建高效、可维护的RESTful服务。实践中,不断优化接口设计,提高API的易用性和性能,是提升开发效率和用户体验的关键。

    Spring整合CXF发布服务

    - 日志和监控:在生产环境中,对服务的运行状态进行日志记录和监控是必要的,可以使用Spring的AOP和CXF的拦截器来实现。 - 性能优化:根据项目需求,可能需要对服务的性能进行优化,如使用HTTP缓存、减少网络通信...

    Spring2+CXF实现webservice笔记

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

    spring+cxf小demo

    总的来说,这个【Spring+CXF小Demo】是一个很好的起点,可以帮助开发者了解如何在Spring环境中使用CXF构建Web服务,并进行客户端调用。它涉及到的技能包括XML配置、注解驱动编程、服务暴露与消费等,对于理解分布式...

    Spring集成Cxf调用WebServices

    8. **异常处理**:Spring和CXF结合时,可以使用Spring的AOP来处理Web服务调用中的异常。例如,可以定义一个切面来捕获并处理CXF抛出的异常。 9. **测试和调试**:在集成测试中,可以使用Spring的TestContext框架和...

    myBatis+spring+cxf 框架简单整合(包jar)

    3. 创建CXF服务:定义Web服务接口,实现该接口并配置到CXF中。CXF提供了基于注解的Web服务开发方式,可以轻松地将接口暴露为Web服务。 4. 发布Web服务:使用CXF的Servlet或者Spring的CXF非Servlet方式来发布Web服务...

Global site tag (gtag.js) - Google Analytics