Home >Database >Mysql Tutorial >CXF入门教程(2)

CXF入门教程(2)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:12:231346browse

文中对应的代码已经上传,与 教程(1) 中的service相对应。为调试方便,将service和client都放在了同一个工程中,不过是在不同的包中;本文对应的两个基本客户端在com.neareast.test.cxf.client.consumer包中,服务端ServiceTest类在com.neareast.test.cxf.

文中对应的代码已经上传,与教程(1)中的service相对应。为调试方便,将service和client都放在了同一个工程中,不过是在不同的包中;本文对应的两个基本客户端在com.neareast.test.cxf.client.consumer包中,服务端ServiceTest类在com.neareast.test.cxf.server.service包中。代码地址如下:

http://download.csdn.net/detail/neareast/4411870

最常见的情境是,我们有一个服务的WSDL;无论这个服务是不是我们自己维护的,我们都可以从这个WSDL文件来生成客户端,包括一个强类型的接口,并通过接口来与服务进行交互。

使用WSDL2Java工具,可以通过WSDL生成JAX-WS客户端。使用WSDL2java有以下三种方法:

  • The command line 命令行
  • The Maven Plugin Maven插件
  • 使用WSDL2Java API

要深入获取更多信息,可以参阅 Developing a JAX-WS consumer 一文或发型包中的示例程序。

Eclipse和Maven的插件最终应该还是调用WSDL2Java命令来实现的。这里我们使用中发布的服务,直接在控制台使用 wsdl2java http://localhost:9000/helloWorld?wsdl 命令,即可在命令所在目录下( 如笔者在 apache-cxf-2.6.1包的bin目录下调用wsdl2java 命令,生成的类文件就在这个目录下),生成与WSDL中的targetNamespace相对应的包路径,将相应的类放到该目录下,客户端直接调用这些类即可;比较无奈的是,生成的代码居然是ANSI格式的……

然而,生成的HelloWorld类报错,如HelloWorld方法的super(WSDL_LOCATION, SERVICE, features);这行报错,原因是javax.xml.ws.Service中缺少Service(URL, QName, WebServiceFeature[]) 构造方法。我们引入的geronimo-jaxws_2.2_spec-1.1.jar包里,提供了新版本的Service类,但是需要进行endorse才能够替换掉jre自带的Service类。为尽量较少以后移植的麻烦,我们可以根据注释中的提示指定-frontend参数,使用JAX-WS 2.1兼容模式重新生成所有的类,顺便用-p参数指定我们需要的命名空间:

wsdl2java -p com.neareast.test.cxf.client.WSDL2Java -frontend jaxws21 http://localhost:9000/helloWorld?wsdl

生成的类如下图所示;其中HelloWorld类用来启动对服务的监听,IHelloWorld类就是调用服务的接口。

CXF入门教程(2)

一旦生成了客户端,典型的用法如下:

public class BasicClient {
	public static void main(String[] args){
		HelloWorld server = new HelloWorld();
		IHelloWorld hello = server.getHelloWorldImplPort();

		String result = hello.sayHi("East");
		System.out.println(result);
		
		User user = new User();
		user.setName("East");
		System.out.println(hello.sayHiToUser(user));
		
		System.out.println("All the users are: ");
		for(IdentifiedUser u:hello.getUsers().getEntry()){
			System.out.println( u.getUser().getName() );
		}
	}
}

至此,一个简单的webService客户端已经完成了。

JAX-WS代理

除了使用使用wsdl2java 直接生成客户端,我们也可以使用 Service.create 来生成服务实例,下面的代码展示了这一过程: 

        URL url = null;
        try {
            url = new URL("http://localhost:9000/helloWorld?wsdl");
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(HelloWorld.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "http://localhost:9000/helloWorld?wsdl");
        }
        WSDL_LOCATION = url;

        Service service = Service.create(WSDL_LOCATION, SERVICE_NAME);
        
        IHelloWorld hw = service.getPort(IHelloWorld.class);
        System.out.println(hw.sayHi("World"));

NearEast

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn