CXF RS和JAX-WS编码内容无法读取

3
我的问题有两个方面,与编码有关。我在JBoss 7.1.1上部署了一个REST Web应用程序,它具有与外部Web服务的适配器,我使用提供的WSDL使用CXF java2wsdl创建了存根。在与此外部Web服务进行请求/响应期间,请求和响应可能包含保加利亚编码内容。
问题在于,我的REST服务和SOAP适配器不能正确发送/消耗此编码内容,而SOAPUI可以。以下是一个例子:
REST调用将参数“Тест Тест”传递为@PathParam("receiverName") String receiverName 标头包含charset=UTF-8
服务器日志使用cxf日志拦截器记录如下:
--------------------------------------
 Inbound Message
----------------------------
ID: 32
Address:     http://127.0.0.1:8080/rest/request/card/10122083/%D0%A2%D0%B5%D1%81%D1%82%20%D0%A2%D0%B5%D1%81%D1%82/1234124143312
Encoding: UTF-8
Http-Method: POST
Content-Type: application/json; charset=UTF-8
    Headers: {Accept=[application/json], accept-encoding=[gzip, deflate], accept-language=    [en-gb,en;q=0.5], cache-control=[no-cache], connection=[keep-alive], Content-Length=[313],     content-type=[application/json; charset=UTF-8], host=[127.0.0.1:8080], pragma=[no-cache],     user-agent=[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1]}
----------------------------

我的代码现在打印到日志中的参数值为"???? ????"

当我再次接收保加利亚语的内容时,后端出现了问题,以下是这样一个响应的示例

Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], 

根据日志和wireshark,请求是带有上述Headers发送的,而响应如下:

-----------------------------------------------------------------
ID: 2
Response-Code: 200
Encoding: ISO-8859-1
Content-Type: text/xml
Headers: {Content-Length=[1011], content-type=[text/xml], Server=[IRS/1.0.18.0 guz]}
Payload: <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loy="http://www.location.bg/Loyalty/">
<soapenv:Header/>
<soapenv:Body>
<loy:CustLoyaltyResponse>
                <OutputRequestID>SCR0000000394</OutputRequestID>
                <OutputTimeStamp>20120918095649992</OutputTimeStamp>
                <StatusCode>0</StatusCode>
                <StatusMessage>Loyal Customer</StatusMessage>
                <CustomerCode>10122083</CustomerCode>
                <CustomerLoyalStatus>1</CustomerLoyalStatus>
                <LoyalLevel>Special</LoyalLevel>
                <CardObject>
                                <CardStatus>0</CardStatus>
                                <CardCustCode>10122083</CardCustCode>
                                <CardNo>100012624110122083</CardNo>
                                <CardState>4</CardState>
                                <Level>Special</Level>
                                <City>Р?Р°С?РЅР°</City>
                                <Postcode>9000</Postcode>
                                <Address>жк. Чайка</Address>
                                <NameOfRecipient>РўРµС?С? РўРµС?С?</NameOfRecipient>
                                <NumberOfRecipient>12345678</NumberOfRecipient>
                                <LastStatusChangeDate></LastStatusChangeDate>
                </CardObject>
</loy:CustLoyaltyResponse>
</soapenv:Body>
</soapenv:Envelope>
-----------------------------------------------------------------

问题可以在以下地方看到:
                                <City>Р?Р°С?РЅР°</City>
                            <Address>жк. Чайка</Address>

在SOAPUI中结果是正确的。
<City>Варна</City>
<Address>жк. Чайка</Address>

任何想法,这个问题是什么,如何解决?
2个回答

2

可能记录日志的工具没有以UTF-8格式输出?如果你在使用log4j,尝试添加

<param name="Encoding" value="UTF-8"/>

到它的配置。


1
最后我查看了我的日志文件类型,发现它不是UTF-8,因此我添加了以下内容到我的log4j配置使其编码为UTF-8。
<param name="Encoding" type="UTF-8" /> 

根据@unhillbilly的建议。此外,我的编辑器(TextPad)不显示UTF-8,所以我换成了它可以显示的Notepad++,并且通过删除@Encoded注释,我得到了“Тест Тест”

现在从JAX-RS正确检索该值,并将其传递给我的JAX-WS客户端适配器(整个日志都显示了这一点),并且在SOAP请求中也正确发送(loginterceptors在SOAP请求的打印输出中显示了这一点,而外部Web服务现在回复的事实似乎证实了这一点)。

Request
Address: http://192.168.3.251:4445/WSDL
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], 
...
<ReceiverName>Тест Тест</ReceiverName>

仍然存在的问题是,当我收到包含保加利亚字符的响应时,日志和REST客户端都无法获得正确的字符编码,但在SOAP UI中进行相同的请求会导致正确的字符显示。
该问题可以在我的日志和我的Rest客户端中看到。
<City>Р?Р°С?РЅР°</City>  
<Address>жк. Чайка</Address>  

虽然在SOAPUI中结果是正确的

<City>Варна</City>  
<Address>жк. Чайка</Address>  

我收到的响应头如下所示。我认为ISO-8859-1可能会影响我,但另一方面它并不能解释为什么SOAP UI可以正确处理它。就我所看到的,我无法从我的传输管道影响Web服务本身返回的编码。

ID: 2 
Response-Code: 200 
Encoding: ISO-8859-1 
Content-Type: text/xml 
Headers: {Content-Length=[1011], content-type=[text/xml], 
Server=[IRS/1.0.18.0 guz]}
Payload: <?xml version="1.0" encoding="UTF-8"?>

1
最终,我创建了一个模拟外部网络服务的Web服务,并返回保加利亚字符,我的服务完美处理。这是由于Header属性Content-Type和Encoding的正确设置,设置如下: Content-Type: text/xml;charset=UTF-8' Encoding: UTF-8而来自外部网络服务的Headers则为**Content-Type: text/xml Encoding: ISO-8859-1** - Stelios Koussouris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接