依赖的JAR
    cxf-2.2.10.jar
    jetty-6.1.21.jar
    jetty-util-6.1.21.jar
    servlet-2_5-api.jar
    wsdl4j-1.6.2.jar
    XmlSchema-1.4.5.jar
创建一个普通的Java工程即可

创建webservice接口

package com.cxf.interfaces;

import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface HelloWorldServiceInf { String sayHello(@WebParam(name="username") String username); }

发布和调用webservice
        方法一
发布webservice

package com.cxf.impl;

import javax.jws.WebService;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.cxf.interfaces.HelloWorldServiceInf; @WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
public class Server implements HelloWorldServiceInf { public String sayHello(String username) {
return "Hello,"+username;
} public static void main(String[] args) {
Server impl=new Server();
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:9000/hello");
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setServiceBean(impl);
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.create();
} }

wsdl描述文件

 <?xml version="1.0" ?>
- <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://interfaces.cxf.com/" 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://interfaces.cxf.com/" xmlns:tns="http://interfaces.cxf.com/" 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="username" 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="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorldServiceInf">
- <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="HelloWorldServiceInfServiceSoapBinding" type="tns:HelloWorldServiceInf">
<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="HelloWorldServiceInfService">
- <wsdl:port binding="tns:HelloWorldServiceInfServiceSoapBinding" name="HelloWorldServiceInfPort">
<soap:address location="http://localhost:9000/hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

客户端调用

package com.cxf.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf; public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setAddress("http://localhost:9000/hello");
HelloWorldServiceInf impl=(HelloWorldServiceInf) factoryBean.create();
System.out.println(impl.sayHello("张三"));
}
}

方法二
发布webservice

package com.cxf.impl;

import javax.jws.WebService;
import javax.xml.ws.Endpoint; import com.cxf.interfaces.HelloWorldServiceInf; @WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
public class Server implements HelloWorldServiceInf { public String sayHello(String username) {
return "Hello,"+username;
}
public static void main(String[] args) {
Server impl=new Server();
String address="http://localhost:9000/hello";
Endpoint.publish(address, impl);
}
}

wsdl文件

 <?xml version="1.0" ?>
- <wsdl:definitions name="helloWorldService" targetNamespace="http://impl.cxf.com/" xmlns:ns1="http://interfaces.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" />
- <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf">
<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="helloWorldService">
- <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="ServerPort">
<soap:address location="http://localhost:9000/hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

客户端调用

package com.cxf.client;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding; import com.cxf.interfaces.HelloWorldServiceInf; public class Client {
//注意:此处http://interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" /> private static final QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
private static final QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
public static void main(String[] args) {
String endPointAddress="http://localhost:9000/hello";
Service service=Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
HelloWorldServiceInf inf=service.getPort(HelloWorldServiceInf.class);
System.out.println(inf.sayHello("张三"));
}
}

CXF根据wsdl文件动态调用WebService

package com.cxf.client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientFromWsdl {

    public static void main(String[] args) throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9000/hello?wsdl");
//sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");
//输出调用结果
System.out.println(objects[0].toString());
}
}

参考原文:http://www.blogjava.net/sxyx2008/archive/2010/09/15/332058.html

使用CXF发布和调用webservice之HelloWorld入门的更多相关文章

  1. Spring整合CXF发布及调用WebService

    这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法 添加jar包 (官方下载地址:http://cxf.apache.org/downlo ...

  2. java中使用axis发布和调用webService及dom4j解析xml字符串

    工作中需要调用webService服务,这里记录一下如何在java中发布和调用webService. 需要的jar包: webService服务端: import javax.jws.WebMetho ...

  3. webService总结(一)——使用CXF公布和调用webService(不使用Spring)

    CXF和Axis2是两个比較流行的webService框架,接下来我会写几篇博客简介怎样使用这两种框架. 首先,先简介一下CXF的使用. CXF公布webService有多种方法.这里我介绍三种: 1 ...

  4. Axis 发布、调用WebService(转)

    一.JWS方法发布WebService 1.在官方网站下载axis的工程(这个等下就有用的)和源码.jar包等,下载地址是: http://www.apache.org/dyn/closer.cgi/ ...

  5. [PHP]php发布和调用Webservice接口的案例

    分两步走:1.服务端发布接口;2.客户端调用方法 1.服务端发布接口: 需要nusoap工具,下载地址:http://sourceforge.net/projects/nusoap/ 下载完和要发布接 ...

  6. 使用cxf 发布 jax-rs 风格webservice 。并客户端测试。

    详细介绍:http://www.ibm.com/developerworks/cn/java/j-lo-jaxrs/ 1.定义一个User对象 package com.zf.test; import  ...

  7. WS的发布与调用

    WebService—CXF整合Spring实现接口发布和调用过程  https://www.cnblogs.com/domi22/p/8094517.html   spring 集成cxf 第二弹( ...

  8. mule发布调用webservice

    mule发布webservice 使用mule esb消息总线发布和调用webservice都非常精简,mule包装了所有操作,你只需要拖控件配置就可以,下面讲解mule发布: 1.下面是flow,h ...

  9. WebService—CXF整合Spring实现接口发布和调用过程

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

随机推荐

  1. HslCommunication库的二次协议扩展,适配第三方通讯协议开发,基础框架支持长短连接模式

    本文将使用一个gitHub开源的项目来扩展实现二次协议的开发,该项目已经搭建好了基础层架构,并实现了三菱,西门子,欧姆龙,MODBUS-TCP的通讯示例,也可以参照这些示例开发其他的通讯协议,并Pul ...

  2. CF1117C Magic Ship

    CF1117C Magic Ship 考虑到答案具单调性(若第 \(i\) 天能到达目的点,第 \(i+1\) 天只需向风向相反的方向航行),可以二分答案. 现在要考虑给出一个天数 \(m\) ,问 ...

  3. Codeforces 25E Test 【Hash】

    Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...

  4. 再也不用克隆多个仓库啦!git worktree 一个 git 仓库可以连接多个工作目录

    我在 feature 分支开发得多些,但总时不时被高优先级的 BUG 打断需要临时去 develop 分一个分支出来解 BUG.git 2.6 以上开始提供了 worktree 功能,可以解决这样的问 ...

  5. 20165222 实验三 敏捷开发与XP实践

    实验内容: 1.XP基础 2.XP核心实践 3.相关工具 二.具体实验内容 (一)代码规范格式化 总结:感觉就那个Code->Reformate Code代码重新格式化最好用,其他的我都点了下, ...

  6. spring注解实现业务层事务管理,当业务层自调用时,事务失效问题解决

    前些日子一朋友在需要在目标对象中进行自我调用,且需要实施相应的事务定义,且网上的一种通过BeanPostProcessor的解决方案是存在问题的.因此专门写此篇帖子分析why. 1.预备知识 aop概 ...

  7. groovy gradle 构建配置

    参考配置 apply plugin: "idea" apply plugin: "groovy" apply plugin: "eclipse&quo ...

  8. gradle 插件

    1. 系统内置插件的应用 a. 二进制 apply plugin :"pluginname" 比如: java b. 脚本插件 apply from : "version ...

  9. 如何在CentOS系统中安装配置SNMP服务

    CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,现在有一大部分服务器在使用此操作系统:SNMP(简单网络 ...

  10. oracle之 调整 I/O 相关的等待

    I/O相关竞争等待简介 当Oracle数据库出现I/O相关的竞争等待的时候,一般来说都会引起Oracle数据库的性能低下,发现数据库存在I/O相关的竞争等待一般可以通过以下的三种方法来查看Oracle ...