利用Apache CXF生成客户端的几种方法【官方原版】

CXF provides you with many options to build clients for your services. This guide is meant to give you a quick overview of those options and help you orient yourself quickly with CXF.

Building Clients

WSDL2Java generated Client

One of the most common scenarios is that where you have a service which you may or not manage and this service has a WSDL. In this case you'll often want to generate a client from the WSDL. This provides you with a strongly typed interface by which to interact with the service. Once you've generated a client, typical usage of it will look like so:

HelloService service = new HelloService();

Hello client = service.getHelloHttpPort();

 

String result = client.sayHi("Joe");

The WSDL2Java tool will generate JAX-WS clients from your WSDL. You can run WSDL2java one of three ways:

For more in depth information read Developing a JAX-WS consumer or see the Hello World demos inside the distribution.

JAX-WS Proxy

Instead of using a wsdl2java-generated stub client directly, you can use Service.create to create Service instances, the following code illustrates this process:

import java.net.URL;

import javax.xml.ws.Service;

...

 

URL wsdlURL = new URL("http://localhost/hello?wsdl");

QName SERVICE_NAME = new QName("http://apache.org/hello_world_soap_http", "SOAPService");

Service service = Service.create(wsdlURL, SERVICE_NAME);

Greeter client = service.getPort(Greeter.class);

String result = client.greetMe("test");

JAX-WS Dispatch APIs

JAX-WS provides the "dispatch" mechanism which makes it easy to dynamically invoke services which you have not generated a client for. Using the Dispatch mechanism you can create messages (which can be JAXB objects, Source objects, or a SAAJMessage) and dispatch them to the server. A simple example might look like this:

import java.net.URL;

import javax.xml.transform.Source;

import javax.xml.ws.Dispatch;

import javax.xml.ws.Service;

...

 

URL wsdlURL = new URL("http://localhost/hello?wsdl");

Service service = Service.create(wsdlURL, new QName("HelloService"));

Dispatch<Source> disp = service.createDispatch(new QName("HelloPort"), Source.class, Service.Mode.PAYLOAD);

 

Source request = new StreamSource("<hello/>")

Source response = disp.invoke(request);

NOTE: you can also use dispatches without a WSDL.

For more in depth information see the Hello World demos inside the distribution.

Simple Frontend Client Proxy

If you've developed a service using the simple frontend, you can use the ClientProxyFactoryBean API to create a Java proxy client for your service. This way you can use the service interface to talk to your service. For more information see the Simple Frontend documentation.

Dynamic Client

CXF includes a Client interface which allows you to invoke operations and pass parameters for those operations. For instance:

Client client = ....;

Object[] result = client.invoke("sayHi", "Dan");

There are two ways to create Clients at runtime. The first choice is to use the ClientFactoryBean or JaxWsClientFactoryBean classes. These will create proxy objects for the SEI for the service. These proxies cannot handle complex objects.

The second to use the DynamicClientFactory or one of its subclasses. The DynamicClientFactory goes the additional step of generating and compiling JAXB POJOs for complex objects described in the WSDL, for use at runtime via reflection.

This is most useful when you're using a dynamic language such as Groovy with CXF, but it is possible to use reflection directly from Java.

More Information: Dynamic Clients

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值