CXF(2.7.10)

以调用 http://www.webxml.com.cn/ 提供的 IpAddressSearchWebService 服务为例。

1. 使用 wsdl2java 工具,根据 wsdl 生成 JAX-WS 客户端

wsdl2java -client "http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl"

2. 将生成代码导入工程。(可能报错,需要修改)

3. 访问服务。

package com.huey.demo.test;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Test;

import cn.com.webxml.ArrayOfString;
import cn.com.webxml.IpAddressSearchWebServiceSoap;

public class Wsdl2javaTest {

    @Test
    public void testWsdl2java() throws Exception {
        System.out.println("Starting Client...");

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(IpAddressSearchWebServiceSoap.class);
        factory.setAddress("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");

        IpAddressSearchWebServiceSoap ipAddressSearchWebService = (IpAddressSearchWebServiceSoap) factory.create();
        ArrayOfString result = ipAddressSearchWebService.getCountryCityByIp("8.8.8.8");
        for (String str : result.getString()) {
            System.out.println(str);
        }
    }

}
原文地址:https://www.cnblogs.com/huey/p/4589090.html