天天看點

用CXF來調用webService用CXF來調用webService

用CXF來調用webService

CXF釋出服務,有三種形式:根據main方法釋出【Java Project項目】;不基于Spring方式釋出到Web容器中,比如Tomcat容器【web項目】;基于Spring方式釋出到Web容器中。【web項目】

1. Java的webService實作本身就是一個很耗性能的實作方案(xml與java對象之間在服務端以及用戶端的互轉比較消耗性能)

2. 目前java主流的webService應用以CXF、AXIS2為主;

3. 通過網絡管道的了解,目前CXF的效率要比AXIS2高出至少50%;

4. CXF在java應用實作中已經很成熟,企業更傾向于用這樣一個成熟的解決方案;

5. CXF和Spring高度內建。

1.通過CXF調用service的條件

<dependency>

   <groupId>org.apache.cxf</groupId>

   <artifactId>cxf-rt-frontend-jaxws</artifactId>

   <version>${cxf.version}</version>

</dependency>

<dependency>

   <groupId>org.apache.cxf</groupId>

   <artifactId>cxf-rt-transports-http</artifactId>

   <version>${cxf.version}</version>

</dependency>

<dependency>

   <groupId>org.apache.cxf</groupId>

   <artifactId>cxf-rt-transports-http-jetty</artifactId>

   <version>${cxf.version}</version>

</dependency>

2.通過CXF調用service的方法

Object[] invoke(String operationName,

              Object... params)

                throws Exception

Invokes an operation synchronously

Parameters:

operationName - The name of the operation to beinvoked. The service namespace will be used when looking up theBindingOperationInfo.

params - The params that matches theparts of the input message of the operation. If the BindingOperationInfosupports unwrapping, it assumes the params are in the "unwrapped"form. If params are in the wrapped form, use invokeWrapped

Returns:

The return values that matche the parts of the outputmessage of the operation

Throws:

Exception