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

WebService CXF 使用例子

 
阅读更多

  学习了一下WebService, 做了个小例子,以供参考:

 

  1. 下载 apache-cxf-2.2.12.zip 包,地址: http://cxf.apache.org/

  2. 创建 java 工程,导入相关的jar包.

 

  3. 创建一个接口(使用注解声明webservice @WebService )

   

package test;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

    public String sayHello(String name);

}

 

4. 实现创建的接口

 

package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("接口实现类!");
		return "Hello "+name+" ! ";
	}

}

 

 5. 创建WebService服务器端

 

  

package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

            /**
               * 创建一个服务端
          */
             public static void main(String[] args) {
                 // 创建一个WebService服务端工厂类
           JaxWsServerFactoryBean factory =new JaxWsServerFactoryBean();
                 //定义webService具体实现类,而不是接口
           factory.setServiceClass(HelloWorldImpl.class);  
	 //发布webServer到这个地址
	factory.setAddress("http://localhost:9001/HelloWorld");
		
	//得到Server,并启动
	Server server= factory.create() ;
	server.start();
            }

}

    启动服务器端程序, IE浏览器中 http://localhost:9001/HelloWorld?WSDL 

     可以看到, 服务器端正常

    

<?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="sayHello" type="tns:sayHello" /> 
- <xsd:complexType name="sayHello">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="sayHello">
  <wsdl:part element="tns:sayHello" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="sayHelloResponse">
  <wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
  <wsdl:input message="tns:sayHello" name="sayHello" /> 
  <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
  <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:binding>
- <wsdl:service name="HelloWorldImplService">
- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
  <soap:address location="http://localhost:9001/HelloWorld" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

 

  6. 创建webService客户端

 

  

package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloClient {

	/**
	 * 创建WebService客户端
	 */
	public static void main(String[] args) {
		// 创建代理工厂类(客户端)
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();	
		factory.setAddress("http://localhost:9001/HelloWorld");
		//客户端定义实现接口
		factory.setServiceClass(HelloWorld.class);
		
		HelloWorld hello= (HelloWorld)factory.create();
		System.out.println(hello.sayHello("中 国"));
 
	}

}

 

 

  最后启动服务器端程序MainServer,客户端HelloClient ,会显示

 

2011-7-24 17:42:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}HelloWorldService from class test.HelloWorld
Hello 中 国 ! 

   此时WebService发布成功!

 

  • 大小: 48.6 KB
分享到:
评论

相关推荐

    webService(基于cxf)的完整例子

    Web服务在IT行业中扮演着重要的角色,它允许不同的应用程序通过网络进行通信和交换数据。CXF是一个流行的开源Java框架,用于...通过学习这个例子,开发者可以掌握CXF的基本使用方法,进一步拓展到更复杂的Web服务场景。

    webservice cxf_demo

    【标题】"webservice cxf_demo" 涉及到的是使用Apache CXF框架创建的Web服务示例项目。在Web服务的世界里,CXF是一个流行的开源工具,它支持SOAP和RESTful风格的服务,用于构建和消费Web服务。这个"CXF_demo"很可能...

    webService CXF集成例子

    在这个"webService CXF集成例子"中,我们将深入探讨如何不依赖Spring框架来使用CXF进行Web服务的开发和测试。 1. **Apache CXF简介**: Apache CXF是一个全面的Web服务框架,它支持多种Web服务标准,如SOAP、WS-*...

    WebService CXF使用说明

    【WebService CXF 使用详解】 WebService CXF 是一个开源框架,用于构建和开发符合 WS-* 标准的 Web 服务。它提供了丰富的功能,包括支持多种协议和服务风格,以及与 Spring 框架的深度集成。CXF 允许开发者轻松地...

    CXF webservice 示例工程(集成spring)

    本示例工程是基于CXF框架构建的一个Webservice应用,该应用集成了Spring框架,以实现更高效的服务管理和依赖注入。CXF是一个开源的Web服务框架,它允许开发者创建和部署SOAP和RESTful服务,同时也支持WS-*标准,如...

    webservice cxf 整合spring例子源代码

    【标题】:Webservice CXF 整合Spring的实例源码解析 在Web服务开发中,Apache CXF是一个广泛使用的开源框架,它提供了创建、部署和管理Web服务的强大功能。CXF不仅支持SOAP,还支持RESTful API,使得开发者能够...

    Java webservice cxf客户端调用demo和服务端

    3. 配置服务:使用CXF提供的注解(如`@WebService`)标记接口和实现类,指定服务的元数据,如服务名、端点地址等。 4. 发布服务:通过CXF的Server类实例化并启动服务,使其可供客户端调用。 **客户端调用:** 1. ...

    SpringBoot开发WebService之CXF示例

    在本文中,我们将深入探讨如何使用SpringBoot集成Apache CXF来开发Web服务。SpringBoot以其简洁的配置和快速的应用启动而受到广大开发者的欢迎,而CXF是一个强大的开源框架,用于构建和消费Web服务。结合这两者,...

    webservice使用cxf的实例

    【标签】:“源码”和“工具”表明了这篇内容可能包括实际的源代码示例和对CXF这个工具的使用介绍。 【压缩包子文件的文件名称列表】:“cxf-webservice-lib2”很可能包含了CXF库的第二个版本,用于支持Web服务的...

    webservice cxf示例工程集成spring

    总结来说,这个"webservice cxf示例工程集成spring"是一个实践性的教程,旨在展示如何使用Apache CXF和Spring框架来构建、管理和测试Web服务。通过这个项目,开发者可以学习到如何将这两个强大的工具结合在一起,...

    springBoot完整整合WebService框架CXF示例

    本示例项目提供了服务发布端和服务调用端两个项目,它们展示了如何在SpringBoot环境中集成和使用CXF框架。下面将详细讲解这个示例中涉及的关键知识点: 1. **SpringBoot**:SpringBoot的核心特性是自动配置,它可以...

    WebService CXF Demo基本使用

    本教程将深入讲解如何使用Apache CXF进行基本的WebService开发,并通过一个名为"WebServiceDemo"的示例来展示其实现过程。 Apache CXF允许开发者以Java API(JAX-WS)或基于注解的方式创建Web服务。JAX-WS是Java...

    cxf发布webservice示例

    webservice示例 springmvc+maven+cxf发布webservice 博客地址 http://blog.csdn.net/joe_storm/article/details/78839150

    webservice_cxf Demo

    这个"webservice_cxf Demo"项目是一个基于 Apache CXF 的 Web 服务示例,旨在帮助开发者理解如何使用 CXF 来创建、部署和消费 Web 服务。在这个示例中,我们将探讨 CXF 的核心概念、工作流程以及如何通过它来实现...

    webservice cxf 服务端客户端传递参数的例子demo

    下面我们将深入探讨"webservice CXF 服务端客户端传递参数的例子demo"。 1. **CXF框架介绍** CXF是一个Java框架,它允许开发者通过简单的API来创建和使用Web服务。CXF支持多种Web服务标准,如WS-I Basic Profile、...

    webservice cxf 开发实战

    【压缩包子文件的文件名称列表】"cxfstudy"很可能包含了一系列的学习资料或示例代码,例如简单的CXF服务端和客户端程序,或者是关于CXF配置和使用的文档。通过研究这些内容,读者可以更直观地了解和实践CXF开发。 ...

    webservice cxf+spring maven项目

    总结来说,"webservice cxf+spring maven项目"是一个适合初学者的示例,它展示了如何利用CXF、Spring和Maven构建、部署和测试Web服务。这个项目涵盖了Web服务的基本概念,以及CXF和Spring在Web服务中的实际应用,为...

    WebService CXF、 Mybatis简单实例

    综上所述,"WebService CXF、 Mybatis简单实例"是一个结合了Web服务开发、数据库操作以及依赖管理和事务控制的综合示例。这个实例对初学者来说是一份很好的学习资料,可以帮助他们理解这些技术的使用方式以及如何在...

    WebService CXF+struts+spring 示例

    在"WebService CXF+Struts+Spring 示例"项目中,这三者如何协同工作呢? - **CXF**:作为Web服务的核心,CXF负责处理Web服务的发布、调用以及协议转换。在Spring环境中,CXF可以通过Spring配置文件进行配置,定义...

    webservice cxf 简单实例

    通过这个简单的例子,你可以了解如何使用Eclipse和Apache CXF来创建、部署和测试Web服务。随着对CXF的深入学习,你将能够处理更复杂的Web服务需求,包括安全性、事务管理和数据交换格式的多样化。

Global site tag (gtag.js) - Google Analytics