CXF 入门实例


先去Apache下载cxf的最新jar包  本人的是 2.3.0

将所有jar加载到类路径下

 

然后:

----------------------------------------------------------------------

定义一个接口

 

  1. package com.cxf.service.iface;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface Helloworld {  
  8.     public String sayHello(@WebParam(name="text") String text);  
  9. }  

 

定义一个实现

  1. package com.cxf.service.impl;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.cxf.service.iface.Helloworld;  
  6.   
  7. @WebService(serviceName="Helloworld")  
  8. public class HelloworldImpl implements Helloworld {  
  9.       
  10.     public String sayHello(String text) {  
  11.         return "hello " + text;  
  12.     }  
  13.   
  14. }  

 

第三步: 发布web service 服务

 

  1. package com.cxf.service.app;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. import com.cxf.service.impl.HelloworldImpl;  
  6.   
  7. public class HelloworldService {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         HelloworldImpl implementor = new HelloworldImpl();  
  14.         String address = "http://localhost:9000/MyService";  
  15.         Endpoint.publish(address, implementor);  
  16.         System.out.println("服务已经发布....");  
  17.     }  
  18.   
  19. }  

 

 

打开浏览器 输入http://localhost:9000/MyService?wsdl

 

  1.   <?xml version="1.0" encoding="UTF-8" ?>   
  2. <wsdl:definitions name="Helloworld" targetNamespace="http://impl.service.cxf.com/" xmlns:ns1="http://iface.service.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  3.   <wsdl:import location="http://localhost:9000/MyService?wsdl=Helloworld.wsdl" namespace="http://iface.service.cxf.com/" />   
  4. <wsdl:binding name="HelloworldSoapBinding" type="ns1:Helloworld">  
  5.   <soap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" />   
  6. <wsdl:operation name="sayHello">  
  7.   <soap:operation soapAction="" style="document" mce_style="document" />   
  8. <wsdl:input name="sayHello">  
  9.   <soap:body use="literal" />   
  10.   </wsdl:input>  
  11. <wsdl:output name="sayHelloResponse">  
  12.   <soap:body use="literal" />   
  13.   </wsdl:output>  
  14.   </wsdl:operation>  
  15.   </wsdl:binding>  
  16. <wsdl:service name="Helloworld">  
  17. <wsdl:port binding="tns:HelloworldSoapBinding" name="HelloworldImplPort">  
  18.   <soap:address location="http://localhost:9000/MyService" />   
  19.   </wsdl:port>  
  20.   </wsdl:service>  
  21.   </wsdl:definitions>  

 

 

抓取有用的信息。。。。

 

wsdl:definitions name="Helloworld".....

 <wsdl:import location="http://localhost:9000/MyService?wsdl=Helloworld.wsdl"namespace="http://iface.service.cxf.com/" />

targetNamespace="http://impl.service.cxf.com/

 

 

客户端调用

  1. package com.cxf.client;  
  2.   
  3. import javax.xml.namespace.QName;  
  4. import javax.xml.ws.Service;  
  5. import javax.xml.ws.soap.SOAPBinding;  
  6.   
  7. import com.cxf.service.iface.Helloworld;  
  8.   
  9. public class HelloworldClient {  
  10.     //  http://impl.service.cxf.com/  
  11.     private static String namespaceURI = "http://iface.service.cxf.com/";   
  12.     private static QName SERVICE_NAME = new QName(namespaceURI, "Helloworld");  
  13.     private static QName PORT_NAME = new QName(namespaceURI,"HelloworldPort");  
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         Service service = Service.create(SERVICE_NAME);  
  19.         String endpointAddress = "http://localhost:9000/MyService";  
  20.         service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);  
  21.         Helloworld hw = service.getPort(Helloworld.class);  
  22.         String info = hw.sayHello("涛哥");  
  23.         System.out.println(info);  
  24.     }  
  25.   
  26. }  

 

注意几点:

1:namespaceURI指的是targetNamespace!

2:PORT_NAME  的第二个参数 是 服务名 + port

而不是<wsdl:port binding="tns:HelloworldSoapBinding" name="HelloworldImplPort">
中的 HelloworldImplPort  这点不是很懂。 先记着就ok

3: 如果namespaceURI SERVICE_NAME PORT_NAME 配置错误, 将会抛出一个

Invalid address. Endpoint address cannot be null.

 

另外 namespaceURI  末尾有一个反斜杠 /

 

4:endpointAddress 区分大小写 。 否则将会抛出一个404

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值