CXF初步教程(续)

刚才想重温一下WebService,找到了之前写的一篇文档,很久之前也分享过一篇文档,这一篇应该是后来整理的。


4 使用CXF编写客户端

4.1 WSDL2Java generated Client

Option

Interpretation

-?,-h,-help

Displays the online help for this utility and exits.

-fe frontend-name

Specifies the frontend. Default is JAXWS. Currently supports only JAXWS frontend and a "jaxws21" frontend to generate JAX-WS 2.1 compliant code.

-db databinding-name

Specifies the databinding. Default is jaxb. Currently supports jaxb, xmlbeans, sdo (sdo-static and sdo-dynamic), and jibx.

-wv wsdl-version

Specifies the wsdl version .Default is WSDL1.1. Currently suppports only WSDL1.1 version.

-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.

-sn service-name

The WSDL service name to use for the generated code.

-b binding-name

Specifies JAXWS or JAXB binding files or XMLBeans context files. Use multiple -b flags to specify multiple entries.

-catalog catalog-file-name

Specify catalog file to map the imported wsdl/schema

-d output-directory

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

-compile

Compiles generated Java files.

-classdir compile-class-dir

Specifies the directory into which the compiled class files are written.

-client

Generates starting point code for a client mainline.

-server

Generates starting point code for a server mainline.

-impl

Generates starting point code for an implementation object.

-all

Generates all starting point code: types, service proxy, service interface, server mainline, client mainline, implementation object, and an Antbuild.xml file.

-ant

Specify to generate an Ant build.xml script.

-autoNameResolution

Automatically resolve naming conflicts without requiring the use of binding customizations.

-defaultValues=[DefaultValueProvider impl]

Specifies that default values are generated for the impl and client. You can also provide a custom default value provider. The default provider isRandomValueProvider

-nexclude schema-namespace [=java-packagename]

Ignore the specified WSDL schema namespace when generating code. This option may be specified multiple times. Also, optionally specifies the Java package name used by types described in the excluded namespace(s).

-exsh (true/false)

Enables or disables processing of implicit SOAP headers (i.e. SOAP headers defined in the wsdl:binding but not wsdl:portType section.) Processing the SOAP headers requires the SOAP binding jars available on the classpath which was not the default in CXF 2.4.x and older. You may need to add a dependency to cxf-rt-binding-soap for this flag to work. Default is false.

-dns (true/false)

Enables or disables the loading of the default namespace package name mapping. Default is true andhttp://www.w3.org/2005/08/addressing=org.apache.cxf.ws.addressing namespace package mapping will be enabled.

-dex (true/false)

Enables or disables the loading of the default excludes namespace mapping. Default is true.

-validate

Enables validating the WSDL before generating the code.

-keep

Specifies that the code generator will not overwrite any preexisting files. You will be responsible for resolving any resulting compilation issues.

-wsdlLocation wsdlLocation

Specifies the value of the @WebServiceClient annotation's wsdlLocation property.

-xjc<xjc args>

Specifies a comma separated list of arguments that are passed directly to the XJC processor when using the JAXB databinding. A list of available XJC plugins can be obtained using-xjc-X.

-noAddressBinding

For compatibility with CXF 2.0, this flag directs the code generator to generate the older CXF proprietary WS-Addressing types instead of the JAX-WS 2.1 compliant WS-Addressing types.

-v

Displays the version number for the tool.

-verbose

Displays comments during the code generation process.

-quiet

Suppresses comments during the code generation process.

-exceptionSuper

superclass for fault beans generated from wsdl:fault elements (defaults to java.lang.Exception)

-reserveClass classname

Used with -autoNameResolution, defines a class names for wsdl-to-javanot to use when generating classes. Use this option multiple times for multiple classes.

-allowElementReferences<=true>

(or -aer) If true, disregards the rule given in section 2.3.1.2(v) of the JAX-WS 2.2 specification disallowing element references when using wrapper-style mapping.

-asyncMethods=foo,bar,...

List of subsequently generated Java class methods to allow for client-side asynchronous calls, similar to enableAsyncMapping in a JAX-WS binding file.

-bareMethods=foo,bar,...

List of subsequently generated Java class methods to have wrapper style (see below), similar to enableWrapperStyle in JAX-WS binding file.

-mimeMethods=foo,bar,...

List of subsequently generated Java class methods to enable mime:content mapping, similar to enableMIMEContent in JAX-WS binding file.

-faultSerialVersionUID <fault-serialVersionUID>

How to generate suid of fault exceptions. Use NONE, TIMESTAMP, FQCN, or a specific number. Default is NONE.

-mark-generated

Adds the @Generated annotation to classes generated.

wsdlurl

The path and name of the WSDL file to use in generating the code.


如:wsdl2java –client XX.wsdl


5 实例教程-WebService 天气预报

首先,我们到网站http://www.webxml.com.cn/zh_cn/web_services.aspx,这个网站有很多可以免费使用的Web服务。
我们这一次做一个天气预报的客户端。
访问网址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx,这里详细介绍了我们可以使用的接口。

5.1 使用wsdl2java 命令生成客户端代码

由于这个WSDL是使用.net发布的,使用Java调用会出现一些问题,所以我们将WSDL文件保存到本地,做适当的修改。
我们试一下,使用wsdl2java –client命令来生成客户端代码,


图 5S:Schema错误

这里的一篇博客,记录了这个错误:

http://www.cnblogs.com/hzhuxin/archive/2011/05/25/2057142.html,

我们将WSDL保存到本地,然后用<s:any minOccurs="2" maxOccurs="2"/>替代<s:elementref="s:schema" /><s:any />

我们使用命令wsdl2java -frontend jaxws21 -client E:\\test.wsdl来生成客户端。

CXF初步教程(续)_第1张图片

5.2 测试开放的接口

CXF初步教程(续)_第2张图片

测试代码:

package org.ygy.client;

import java.util.List;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Before;
import org.junit.Test;
import org.ygy.weather.ArrayOfString;
import org.ygy.weather.WeatherWebServiceSoap;
import org.ygy.weather.GetSupportDataSetResponse.GetSupportDataSetResult;


public class WeatherClient {
	private WeatherWebServiceSoap client = null;
	
	@Before
	public void before() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		
		factory.setAddress("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
		factory.setServiceClass(WeatherWebServiceSoap.class);
		
		client = (WeatherWebServiceSoap) factory.create();
	}
	
	@Test
	public void testGetSupportCity() {
		ArrayOfString result = client.getSupportCity("辽宁");
		
		for(String each : result.getString()) {
			System.out.println(each);
		}
	}
	
	@Test
	public void testGetSupportDataSet() {
		GetSupportDataSetResult temp = client.getSupportDataSet();

		for(Object each : temp.getAny()) {
			System.out.println(each);
		}
	}
	
	@Test
	public void testGetSupportProvince() {
		ArrayOfString result = client.getSupportProvince();
		
		for(String each : result.getString()) {
			System.out.println(each);
		}
	}
	
	@Test
	public void testGetWeatherByCityName() {
		ArrayOfString result = client.getWeatherbyCityName("青岛");
		
		List<String> list = result.getString();
		for(int index = 0; index < list.size(); index ++) {
			System.out.println(index + "," + list.get(index));
		}
	}
}

上面的测试,除了testGetSupportDataSet()得到的结果不正确,其他的测试都正确。
到这里,我们就差不多完成了。
因为,省市的数据不会经常改变,所以我们可以将省,市的数据保存起来,这样就不需要每一次都通过网络连接去获取,直接从本地拿比较快。

我们唯一要做的就是,通过接口 getWeatherByCityName(),来获取天气信息了。


你可能感兴趣的:(webservice,天气预报,CXF)