问题 WebService开发调用正常,部署后调用出错
问题描述
SpringBoot中引用Apache的cxf的jar包,执行WebService调用请求,本地开发测试无问题,项目打包放入服务器执行报错
错误信息
java.lang.IllegalStateException:Unable to create schema compiler
解决方法
会发现缺少了一个类,这个类是在com.sun.tools下面的类,那么看到sun我们首先要意识到这是jdk里面的东西。那么这个时候去找jdk/lib发现有个tools.jar,把它解压往目录里面找,就会找到我们缺少的那个类;我们知道java项目在运行的时候是在jre中运行的,就是说打包的jar项目在jre运行的时候也需要tools.jar里面的类的,但是我们去jre/lib去找,却发现jre/lib没有tools.jar,问题原因找到了,就是:jre运行环境缺少tools.jar,于是我们将jdk里面的tools.jar复制带jre/lib之后再去启动,发现webservice能够正常调用。
问题 编码GBK的不可映射字符
问题描述
使用CXF调用WebService服务出现错误: 编码GBK的不可映射字符
错误信息

报错原因:
DynamicClientFactory动态编译时对中文不兼容,导致乱码的发生,需要修改源码才能解决。
解决方法:
在项目中新增一类继承DynamicClientFactory,然后覆写compileJavaSrc。
package com.viewhigh.oes.config;import org.apache.cxf.Bus;import org.apache.cxf.bus.CXFBusFactory;import org.apache.cxf.endpoint.EndpointImplFactory;import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;import org.apache.cxf.jaxws.support.JaxWsEndpointImplFactory;import java.io.File;import java.util.List;/*** 复写DynamicClientFactory,解决cxf调用webservice出现错误的GBK的不可映射的字符* @author ZhangXin* @date 2020-11-23 10:19*/public class JaxWsDynamicClientFactory extends DynamicClientFactory {protected JaxWsDynamicClientFactory(Bus bus) {super(bus);}@Overrideprotected EndpointImplFactory getEndpointImplFactory() {return JaxWsEndpointImplFactory.getSingleton();}@Overrideprotected boolean allowWrapperOps() {return true;}/*** Create a new instance using a specific <tt>Bus</tt>.** @param b the <tt>Bus</tt> to use in subsequent operations with the* instance* @return the new instance*/public static JaxWsDynamicClientFactory newInstance(Bus b) {return new JaxWsDynamicClientFactory(b);}/*** Create a new instance using a default <tt>Bus</tt>.** @return the new instance* @see CXFBusFactory#getDefaultBus()*/public static JaxWsDynamicClientFactory newInstance() {Bus bus = CXFBusFactory.getThreadDefaultBus();return new JaxWsDynamicClientFactory(bus);}/*** 覆写父类的该方法<br/>* 注:解决此(错误:编码GBK的不可映射字符)问题*/@Overrideprotected boolean compileJavaSrc(String classPath, List<File> srcList, String dest) {org.apache.cxf.common.util.Compiler javaCompiler= new org.apache.cxf.common.util.Compiler();// 设置编译编码格式(此处为新增代码)javaCompiler.setEncoding("UTF-8");javaCompiler.setClassPath(classPath);javaCompiler.setOutputDir(dest);javaCompiler.setTarget("1.6");return javaCompiler.compileFiles(srcList);}}
在方法调用中,调用复写的JaxWsDynamicClientFactory类

