关于使用cxf调用webservice出现的问题

在近期作了个小的项目,须要调用其余系统的webservice。刚开始随便网上搜了个cxf调用,出现了不少问题。java

在项目经过开发工具开发完成后,经过eclipes启动项目自测。成功接到数据处理,没有问题。web

而后想springboot项目打成jar,在dos窗口运行。spring

调用代码:apache

// 建立动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://10.7.101.43:9081/contract/service/IActTransactionWebService?wsdl");
                    //调用webservice                    
                    // 须要密码的状况须要加上用户名和密码
                    // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
                    Object[] objects = new Object[0];
                     
                    try {
                        // invoke("方法名",参数1,参数2,参数3....);
                        objects = client.invoke("*", *,*); springboot

问题出现了。报错:编码GBK的不可映射字符app

而后百度问题,找到解决办法ide

1.开发环境和Tomcat都统一设置编码方式为UTF-8。工具

弄了半天,仍是解决不了。而后就使用第二种。开发工具

2.DynamicClientFactory动态编译时对中文不兼容,致使乱码的发生,须要修改源码才能解决。有两种解决方法:一是将DynamicClientFactory.class进行反编译,修改代码后编译,而后覆盖jar包中的该文件;二是在项目中新增一类继承DynamicClientFactory,而后覆写compileJavaSrc。编码

这里推荐第二种方法解决
 源码DynamicClientFactory类中有个下面的方法:

 

只须要在其中加入

// 设置编译编码格式(此处为新增代码)
        javaCompiler.setEncoding("UTF-8");  这段代码便可解决。

因此本身建立JaxWsDynamicClientFactory继承DynamicClientFactory类重写compileJavaSrc方法

本身的JaxWsDynamicClientFactory以下:

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

}

而后运行项目完美解决。

相关文章
相关标签/搜索