Java调用 webService 接口报错

问题描述

本文采用的是 jdk1.8.0_202,装在 D 盘中,配置了系统环境变量 path。

D:\Java\jdk8\bin;D:\Java\jdk8\jre\bin;

本文调用 webservice 的依赖如下:

        <!-- webservice调用依赖cxf-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
        <!-- webservice调用依赖axis-->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

调用方法:

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsUrl);

日志报错:

java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompiler(JAXBUtils.java:711)
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompilerWithDefaultAllocator(JAXBUtils.java:725)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createSchemaCompiler(DynamicClientFactory.java:423)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:307)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:241)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:234)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:189)

解决流程

先按照网络上热门的解决方案,将 jdk/lib/tools.jar 复制到 jdk 同级的 jre/lib 下面。
注意:要打开文件夹隐藏目录显示,才能看到 tools.jar 。

然后又发送请求,报错如下:

java.lang.NullPointerException: null
	at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:187)
	at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:141)
	at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:136)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.compileJavaSrc(DynamicClientFactory.java:611)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:370)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:241)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:234)
	at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:189)

包 cxf 空指针,我看了一下我的环境变量配置,分别配置的是 jdk/bin 和 jdk/jre/bin ,那我直接把 tools.jar 拷进 jdk/jre/lib 试试,发现还是报 cxf 空指针。

我看到网上有人说,

然后,我就在 dcf.createClient(wsUrl) 初始化 Client 之前,加了这一段代码:

// xxx是当前类的名字
Thread.currentThread().setContextClassLoader(xxx.class.getClassLoader());

发现还是空指针,于是我改成了这个:

Thread.currentThread().setContextClassLoader(Client.class.getClassLoader());

仍然不行。

解决方案

于是我更换为 jdk1.8.0_341 (点击使用夸克网盘下载),并使用默认安装路径(C:\Program Files\Java,这样不用去配置环境变量)。

然后继续上述步骤,把 jdk/lib 下的 tools.jar 分别复制到 jdk/jre/lib 和 jdk 同级的 jre/lib 下面。成功解决问题。

NICE NICE NICE NICE !!!

小结: 注意版本问题,且要将 tools.jar 复制到 jre/lib 中。

  • apache-cxf-3.2.XX 开始JDK最低要求要JDK1.8
  • apache-cxf-3.1.XX还是支持JDK1.7的
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

364.99°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值