JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); String wsUrl = "http://www.xxx.com/order.service?wsdl"; Client client = dcf.createClient(wsUrl);
在 Windows 系统的使用 CXF 动态客户端时可能会遇到 tomcat 启动后调用 wsdl 遇到 不少 错误GBK编码,这个错误的缘由是 因为项目 maven 配置使用 UTF-8 的,CXF 生成java 文件是使用的UTF-8 的编码,而使用javac 编译的时候 取的是系统默认的编码 因为中文 window 系统采用GBK 编码,全部就至关于使用 javac -encoding gbk *.java,全部就出 出现 如上错误,正确的 方法是 javac -encoding UTF-8 *.java就能够解决。java
查询 org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory,有误编码相关设置,可选没提供,继续寻找它的父类 org.apache.cxf.endpoint.dynamic.DynamicClientFactory,很惋惜也没有。继续研读源码看他们的编译过程,在 DynamicClientFactory 的 发现 compileJavaSrc 方法 以下:apache
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.setClassPath(classPath); javaCompiler.setOutputDir(dest); javaCompiler.setTarget("1.6"); return javaCompiler.compileFiles(srcList); }
继续 org.apache.cxf.common.util.Comilper :tomcat
重要找到解决办法了从新实现类 DynamicClientFactory 的 compileJavaSrc 方法maven
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);
}
这样就大功告成。编码
固然还有一种解决思路,既然是 file.encoding 的 问题,咱们能够在tomcat 中设置spa
-Dfile.encoding=UTF-8,这样编译器的控制台一样须要设置为 -Dfile.encoding=UTF-8,也能够解决。.net
详情见: IntelliJ IDEA 控制台中文乱码解决方案 blog