XML:使用cxf调用WebService接口时报错:编码GBK的不可映射字符(设置UTF-8字符集)

调用代码如下

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(PropertiesUtil.getValue("sms.requrl"));
Object[] objects = client.invoke("SendNote", phoneNo, content,
            PropertiesUtil.getValue("sms.username"), PropertiesUtil.getValue("sms.userpwd"),
            PropertiesUtil.getValue("sms.comid"), "", PropertiesUtil.getValue("sms.smsnumber"));
// 获取响应码
String respCode = objects[0].toString();
log.info("发送短信响应结果:" + SmsConstants.RESPONSE_CODE.get(respCode));

测试时出现如下错误(中文乱码)

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 绫汇??
                                       ^
...... 此处省略

注:开发环境和Tomcat都统一设置编码方式为UTF-8。

报错原因:

DynamicClientFactory动态编译时对中文不兼容,导致乱码的发生,需要修改源码才能解决。

解决方法:

在项目中新增一类继承DynamicClientFactory,然后覆写compileJavaSrc。
/**
 * 覆写父类的compileJavaSrc方法,解决动态编译乱码问题
 *
 * @author yueli.liao
 * @date 2019-03-08 14:10
 */
public class JaxWsDynamicClientFactory extends DynamicClientFactory {

    protected JaxWsDynamicClientFactory(Bus bus) {
        super(bus);
    }

    @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);
    }

}

引用如上实现类JaxWsDynamicClientFactory进行操作。

文章转载至:https://www.jianshu.com/p/3e8dfe71475a

猜你喜欢

转载自www.cnblogs.com/nhdlb/p/12968749.html
今日推荐