美文网首页
使用cxf调用WebService接口时出错,错误:编码GBK的

使用cxf调用WebService接口时出错,错误:编码GBK的

作者: 跳动的字符 | 来源:发表于2019-03-08 15:15 被阅读0次

 最近在公司开发短信发送接口,第三方接口为WebService接口,采用apache cxf工具包进行开发,现在遇到的问题及解决方法记录下来,以备查用。

此工具包需引用如下jar包:

dependencies {
    compile (
            // cxf support
            'org.apache.cxf:cxf-core:3.1.18',
            'org.apache.cxf:cxf-rt-frontend-jaxws:3.1.18',
            'org.apache.cxf:cxf-rt-transports-http-jetty:3.1.18'
    )

 注:此项目为gradle web项目,其他引用方式请自行百度或Google。

调用代码如下:

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.class进行反编译,修改代码后编译,然后覆盖jar包中的该文件;二是在项目中新增一类继承DynamicClientFactory,然后覆写compileJavaSrc。

 对于gradle和maven管理的项目来说,第一种方式是不可行的,只有第二次方法是最好的解决方法,修改后的代码如下:

/**
 * 覆写父类的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);
    }

}

采用此方法完美解决乱码问题,并测试通过。撒花......

相关文章

网友评论

      本文标题:使用cxf调用WebService接口时出错,错误:编码GBK的

      本文链接:https://www.haomeiwen.com/subject/wlsrpqtx.html