使用CXF和camel-cxf调用webservice

本文详细介绍了如何使用Apache CXF和camel-cxf调用WebService。首先解释了CXF作为WebService框架的功能,包括支持的协议和标准。接着讲解了CXF调用WebService的两种方式——Provider-Dispatch和JAX-WS,特别是从WSDL到Java的转换。最后,文章阐述了如何在Camel中使用camel-cxf组件,包括URI格式、选项、DataFormats以及Interceptor和Feature的使用。
摘要由CSDN通过智能技术生成

CXF是什么

Apache CXF 是一个开源的、全功能的WebService框架,它提供了一套工具和API来帮助开发和构建WebService,像 JAX-WS 和 JAX-RS。它也支持许多WebService标准,例如:

和许多其他的特性。[更多的请参考 http://en.wikipedia.org/wiki/Apache_CXF]

demo.wsdl 是本文中使用的示例wsdl文件.

使用CXF调用WebService

每个WebService服务都需要提供一套用于外部调用的接口,也需要提供对应于这些接口调用的输入输出数据格式。用于规范这些接口和消息格式定义的标准就是 WSDL 。在本节里,将讨论如何使用CXF 来开发一个基于某个WSDL的WebService客户端。 (这里假设使用的传输协议为 HTTP,更多的可以参考本博客里的关于CXF的介绍的文章).

本质上,每个基于 HTTP 的WebService服务都等价于一个接收 HTTP-POST 请求的HTTP服务,为了调用这个服务,用户只需要知道如何构造一个期望的HTTP POST请求格式,例如:

 

POST http://localhost:8040/services/WebService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://www.talend.org/service/WebServiceOperation1"
Content-Length: 307
Host: localhost:8040
Connection: Keep-Alive
 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.talend.org/service/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:WebServiceOperationRequest1>
         <in>Hello</in>
      </ser:WebServiceOperationRequest1>
   </soapenv:Body>
</soapenv:Envelope>
 可以使用Java  HTTP API 发送以上请求:

 

 

//request content
String content = "<soapenv:Envelope"
        + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""
        + " xmlns:ser=\"http://www.talend.org/service/\">"
        + "<soapenv:Body><ser:WebServiceOperationRequest1><in>Hello</in>"
        + "</ser:WebServiceOperationRequest1></soapenv:Body>"
        + "</soapenv:Envelope>";
 
//service url
URL url = new URL("http://localhost:8040/services/WebService");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
//set soapaction
connection.addRequestProperty("SOAPAction",
        "http://www.talend.org/service/WebServiceOperation1");
connection.setDoOutput(true);
connection.setDoInput(true);
 
//send out request
OutputStream os = connection.getOutputStream();
os.write(content.getBytes());
os.flush();
 
//read response and print out
InputStream is = connection.getInputStream();
int available = is.available();
byte[] response = new byte[available];
is.read(response);
System.out.println(new String(response));
 
os.close();
is.close();

 

从以上代码里可以看出,为了使得调用成功,我们需要关心从请求信息到连接信息的几乎每个细节。因此这里,我们将介绍如果使用CXF 来简化请求,从而让用户可以只关注业务逻辑部分,再底层细节由CXF 处理。

CXF 提供了好几种调用WebService的方法,详细的可以参考本博客里的相关文章。这里就介绍两种:Provider-Dispatch 和 JAX-WS

 Provider-Dispatch

Provider-Dispatch 是一种类似于 Java HTTP 的方式,好处就是灵活、方便、强大,缺点就是和Java HTTP 一样,用户需要知道如何构建一个合格的消息。更多的可以看 http://liugang594.iteye.com/blog/1964510  。

JAX-WS

JAX-WS 是 Java API for XML Web Services 的简称,它是用于创建WebService的Java API,它定义了一个用于Java-WSDL映射的标准,例如WSDL接口如何绑定到Java方法,以及SOAP消息如何匹配到方法的参数等。

为了简化 WSDL 和 Java的映射, CXF 提供了一些工具和API用于Java和WSDL之间的转换,这里只展示如何从WSDL转成Java.

WSDL2Java 命令行

CXF 提供了一个 wsdl2java 命令行工具用于从 WSDL 转换到 Java,有很多可用的选项,以下列出了主要的几个选项:[更多的可以访问 https://cxf.apache.org/docs/wsdl-to-java.html ]:

Option

Interpretation

-p [ wsdl-namespace= ] PackageName

Specifies zero, or more, package names to use for the generated code. Optionally specifies the WSDL namespace to package name mapping.

-d output-directory

Specifies the directory into which the generated code files are written.

wsdlurl

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值