1问题描述:
在用cxf创建client客户端的时候,在main方法启动,创建客户端是正常的,但是在tomcat中启动的时候,执行以下第三行代码会出现 警告:编码 GBK 的不可映射字符的 问题:
String wsdlurl = "http://" + "192.168.0.199:8081" + "/services/hello?wsdl";
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = clientFactory.createClient(wsdlurl);
2.控制台出现问题
D:\JavaProgram\Tomcat\apache-tomcat-8.5.12\temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@3c01e324-1552027517229-src\org\tempuri\AddNewUser.java:12: 错误: 编码GBK的不可映射字符
* <p>anonymous complex type鐨? Java 绫汇??
^
D:\JavaProgram\Tomcat\apache-tomcat-8.5.12\temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@3c01e324-1552027517229-src\org\tempuri\AddNewUser.java:12: 错误: 编码GBK的不可映射字符
* <p>anonymous complex type鐨? Java 绫汇??
^
D:\JavaProgram\Tomcat\apache-tomcat-8.5.12\temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@3c01e324-1552027517229-src\org\tempuri\AddNewUser.java:12: 错误: 编码GBK的不可映射字符
* <p>anonymous complex type鐨? Java 绫汇??
^
3.解决方案
DynamicClientFactory动态编译时对中文不兼容,导致乱码的发生,需要修改源码才能解决。有两种解决方法:一是将DynamicClientFactory.class进行反编译,修改代码后编译,然后覆盖jar包中的该文件;二是在项目中新增一类继承DynamicClientFactory,然后覆写compileJavaSrc
对于gradle和maven管理的项目来说,第一种方式是不可行的,只有第二次方法是最好的解决方法,修改后的代码如下:
import java.io.File;
import java.util.List;
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;
public class JaxWsDynamicClientFactory extends DynamicClientFactory {
protected JaxWsDynamicClientFactory(Bus bus) {
super(bus);
// TODO Auto-generated constructor stub
}
@Override
protected EndpointImplFactory getEndpointImplFactory() {
return JaxWsEndpointImplFactory.getSingleton();
}
protected 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的不可映射字符)问题
*
* @return
*/
@Override
protected 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);
}
}
注:修改tomcat编码也可以解决,但会导致其它乱码。
参考:https://blog.csdn.net/liupantao/article/details/78465313
https://www.jianshu.com/p/3e8dfe71475a
https://blog.csdn.net/qunwuhui/article/details/97135382/