温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

依赖的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());
    }
}
下载工程代码
posted on 2010-09-15 11:18 雪山飞鹄 阅读(36237) 评论(12)  编辑  收藏 所属分类: webservice

Feedback

# re: 使用CXF发布和调用webservice之HelloWorld入门 2010-10-18 17:20 西木头
谢谢你啊!看到个写这么清楚的额真不容易。。。  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2010-10-18 18:02 西木头
哎 又有新问题了 别人给的是WSDL 客户端CXF写的话怎么个步骤啊  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2010-10-20 16:05 邓志容
@西木头
我觉得有2中简单用法:
1、用cxf包中的wsdl2java来生成java代码。关于cxf中的wsdl2java用法,你在网上查下,很多介绍。
2、就是博主的最后一个方法,我试了下,用起来非常简单。  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2012-11-15 17:23 paco fan
非常感谢,帮了我大忙了!  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门[未登录] 2012-12-05 15:15 aaaa
cxf-2.2.10.jar包都过期了,2.7.0环境的教程有么?
  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2012-12-07 15:09 啊啊啊
会报这个错误~
十二月 07, 2012 3:09:06 下午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames
INFO: Created classes: com.cxf.interfaces.ObjectFactory, com.cxf.interfaces.SayHello, com.cxf.interfaces.SayHelloResponse
java.lang.NullPointerException
at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:189)
at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:143)
at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:138)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.compileJavaSrc(DynamicClientFactory.java:598)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:367)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:235)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:228)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:183)
at com.cxf.client.ClientFromWsdl.main(ClientFromWsdl.java:11)  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2013-03-15 15:50 dbdebuger
@啊啊啊

原因是你需要使用java安装目录下面的JDK里面的jre  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2014-05-26 17:33 老王头
经过验证,这个确实好用。调用部署在AWS上的webservice成功。2014/05/26  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2014-11-06 17:03 淡淡道
@dbdebuger
123  回复  更多评论
  

# Hello[未登录] 2015-06-08 15:39 张三
Good  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门 2015-09-24 11:24 aabbcc
@张三
警告: [options] 未与 -source 1.5 一起设置引导类路径
着是啥问题呢,用的动态调用  回复  更多评论
  

# re: 使用CXF发布和调用webservice之HelloWorld入门[未登录] 2016-04-20 15:11 薛勇
@邓志容
我使用动态客户端的时候报错了:Created classes: com.xy.cxf.ws.ObjectFactory, com.xy.cxf.ws.SayHello, com.xy.cxf.ws.SayHelloResponse
org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.ws.cxf.xy.com/}sayHello.
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:347)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:341)
at com.cxf.client.JaxWsDynamicClient.main(JaxWsDynamicClient.java:12)
怎么破?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: