关于使用CXF出现编码 GBK 的不可映射字符问题解决

这是Web Service服务端的接口
package org.my.mycxf;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
 
/**
 * CXF Web Service的接口
 * 
 * @author CJY
 * 
 */
// targetNamespace默认是报名的倒写http://mycxf.my.org/
@WebService
public interface HelloWorld {
    @WebMethod(operationName = "SayHello")
    @WebResult(name="word")
    public String sayHello(@WebParam(name = "name") String name,
            @WebParam(name = "address") String address,
            @WebParam(name = "email") String email);
}


这是接口的实现
package org.my.mycxf;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
 
/**
 * HelloWorld的实现类
 * @author CJY
 *
 */
@WebService(endpointInterface="org.my.mycxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
    @WebMethod(operationName = "SayHello")
    @WebResult(name="word")
    @Override
    public String sayHello(@WebParam(name = "name") String name,
            @WebParam(name = "address") String address,
            @WebParam(name = "email") String email) {
        // TODO Auto-generated method stub
        return "你好:"+name+";你的地址是:"+address+";邮箱是:"+email;
    }
     
}



这是客户端的调用
package org.my.mycxf;
 
import javax.xml.namespace.QName;
 
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JaxWsDynamicClientFactory factory=JaxWsDynamicClientFactory.newInstance();
        Client client=factory.createClient("http://localhost:8080/MyCXFAnnotation/ws/helloService?wsdl");
        QName qName=new QName("http://mycxf.my.org/","SayHello");
        try {
            Object[] result=client.invoke(qName, new Object[]{"张三","青岛","[email protected]"});
            System.out.println(result[0].toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
}



但是在客户端调用会有异常
2015-2-1 18:43:06 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames
信息: Created classes: org.my.mycxf.ObjectFactory, org.my.mycxf.SayHello, org.my.mycxf.SayHelloResponse
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:10: 警告:编码 GBK 的不可映射字符
 * 

SayHello complex type鐨?Java 绫汇?
                            ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:10: 警告:编码 GBK 的不可映射字符
 * 

SayHello complex type鐨?Java 绫汇?
                                    ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:12: 警告:编码 GBK 的不可映射字符
 * 

浠ヤ笅妯″紡鐗囨鎸囧畾鍖呭惈鍦ㄦ绫讳腑鐨勯鏈熷唴瀹广?
                                   ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:43: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇name灞炴?鐨勫?銆?
                ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:43: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇name灞炴?鐨勫?銆?
                   ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:43: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇name灞炴?鐨勫?銆?
                     ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:55: 警告:编码 GBK 的不可映射字符
     * 璁剧疆name灞炴?鐨勫?銆?
                ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:55: 警告:编码 GBK 的不可映射字符
     * 璁剧疆name灞炴?鐨勫?銆?
                   ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:55: 警告:编码 GBK 的不可映射字符
     * 璁剧疆name灞炴?鐨勫?銆?
                     ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:67: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇address灞炴?鐨勫?銆?
                   ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:67: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇address灞炴?鐨勫?銆?
                      ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:67: 警告:编码 GBK 的不可映射字符
     * 鑾峰彇address灞炴?鐨勫?銆?
                        ^
C:\Users\ADMINI~1\AppData\Local\Temp\org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory@523be-1422787386518-src\org\my\mycxf\SayHello.java:79: 警告:编码 GBK 的不可映射字符
     * 璁剧疆address灞炴?鐨勫?銆?
                   ^

org.apache.cxf.binding.soap.SoapFault: Cannot create a secure XMLInputFactory
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:84)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:51)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:40)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:849)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1626)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1515)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1318)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:632)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:572)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:481)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:382)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:335)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:355)

at org.my.mycxf.Test.main(Test.java:15)                 


--------------------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------------------------------------------

如下是解决方法:

目前笔记本上的项目问题已经解决

1.第一步将服务端web项目的默认编码设置成UTF-8

右键项目名称properties>resource>text file encoding>other>UTF-8

2.在web.xml中加入:



encodingFilter

org.springframework.web.filter.CharacterEncodingFilter



encoding

UTF-8





forceEncoding

true







encodingFilter

/*



3.在客户端项目中

右键项目名称properties>resource>text file encoding>other>UTF-8

然后项目重新编译一下问题就解决了

 

另外请确定服务端的jar包是否齐全如果不齐会报错:Cannot create a secure XMLInputFactory 



你可能感兴趣的:(CXF)