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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM