使用cxf调用WebService接口时出错,错误:编码GBK的不可映射字符

1问题描述:

在用cxf创建client客户端的时候,在main方法启动,创建客户端是正常的,但是在tomcat中启动的时候,执行以下第三行代码会出现 警告:编码 GBK 的不可映射字符的 问题:

String wsdlurl = "http://" + "192.168.0.199:8081" + "/services/hello?wsdl";
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = clientFactory.createClient(wsdlurl);

2.控制台出现问题

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 绫汇??
                                       ^

3.解决方案

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

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

import java.io.File;
import java.util.List;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.CXFBusFactory;
import org.apache.cxf.endpoint.EndpointImplFactory;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.jaxws.support.JaxWsEndpointImplFactory;

public class JaxWsDynamicClientFactory extends DynamicClientFactory {

	protected JaxWsDynamicClientFactory(Bus bus) {
		super(bus);
		// TODO Auto-generated constructor stub
	}
	@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);
    }
}

注:修改tomcat编码也可以解决,但会导致其它乱码。

参考:https://blog.csdn.net/liupantao/article/details/78465313

https://www.jianshu.com/p/3e8dfe71475a

https://blog.csdn.net/qunwuhui/article/details/97135382/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache CXF是一个开源的WebService框架,可以帮助用户快速、简便地开发和部署WebService应用程序。它提供了多种方式来调用WebService接口,下面介绍几种常用的方式: 1. 使用JAX-WS API:CXF实现了JAX-WS API,可以直接使用JAX-WS提供的API来调用WebService。示例代码如下: ```java HelloWorldService service = new HelloWorldService(); HelloWorld port = service.getHelloWorldPort(); String result = port.sayHello("CXF"); ``` 2. 使用代理方式:CXF可以根据WebService WSDL文件自动生成代理类,然后通过调用代理类的方法来调用WebService接口。示例代码如下: ```java JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String result = client.sayHello("CXF"); ``` 3. 使用Spring配置文件:CXF提供了Spring配置文件方式来实现WebService接口调用。用户可以在Spring配置文件中配置WebService客户端,然后通过Spring容器来获取WebService客户端实例。示例代码如下: ```xml <jaxws:client id="helloClient" serviceClass="com.example.HelloWorld" address="http://localhost:8080/HelloWorld"/> ``` ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String result = client.sayHello("CXF"); ``` 以上是几种常用的调用WebService接口的方式,可以根据具体情况选择适合自己的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值