WSO2ESB Rest API 错误:无效的 UTF-8 中间字节 0x6f(在字符 #143,字节 #127) [英] WSO2ESB Rest API error: Invalid UTF-8 middle byte 0x6f (at char #143, byte #127)
问题描述
我在 wso2esb 5.0.0 中开发了一个 REST API,当客户端发送包含 ã
、ç
或 é
的请求时,我遇到了一些问题代码>例如,inSequence管道上的第一个中介,引发关于无效UTF-8中间字节
的错误.
I developed a REST API in wso2esb 5.0.0 and I am facing some problems when client send a request that contains ã
, ç
or é
for example, the first mediator on inSequence pipeline, Raise an error about Invalid UTF-8 middle byte
.
当我们将 HTTP 标头中的消息字符集强制为 ISO-8859-1 时,工作正常.但我们需要使用 UTF-8.
When we force charset of message in HTTP header as ISO-8859-1, works fine. But we need to work with UTF-8.
有人已经遇到了这个问题并且可以帮助我吗?
Somebody already face this issue and could help me?
跟踪堆栈跟踪错误:
TID[-1234] [ESB] [2017-07-04 15:12:26,738] ERROR {API_LOGGER.ResourceAPI} - Could not build full log message: [com.ctc.wstx.exc.WstxLazyException] Invalid UTF-8 middle byte 0x6f (at char #143, byte #127) com.ctc.wstx.exc.WstxLazyException.throwLazily(WstxLazyException.java:45) com.ctc.wstx.sr.StreamScanner.throwLazyError(StreamScanner.java:720) com.ctc.wstx.sr.BasicStreamReader.safeFinishToken(BasicStreamReader.java:3677) com.ctc.wstx.sr.BasicStreamReader.getText(BasicStreamReader.java:858) org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.getText(XMLStreamReaderWrapper.java:164) org.apache.axiom.om.impl.builder.StAXBuilder.createOMText(StAXBuilder.java:289) org.apache.axiom.om.impl.builder.StAXBuilder.createOMText(StAXBuilder.java:250) org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:252) org.apache.axiom.om.impl.llom.OMElementImpl.buildNext(OMElementImpl.java:653) org.apache.axiom.om.impl.llom.OMElementImpl.getFirstOMChild(OMElementImpl.java:670) org.apache.axiom.om.impl.llom.OMElementImpl.getChildren(OMElementImpl.java:352) org.apache.axiom.om.impl.util.OMSerializerUtil.serializeChildren(OMSerializerUtil.java:553) org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:875) org.apache.axiom.om.impl.util.OMSerializerUtil.serializeChildren(OMSerializerUtil.java:555) org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:875) org.apache.axiom.om.impl.util.OMSerializerUtil.serializeChildren(OMSerializerUtil.java:555) org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:875) org.apache.axiom.soap.impl.llom.SOAPEnvelopeImpl.internalSerialize(SOAPEnvelopeImpl.java:230) org.apache.axiom.om.impl.llom.OMSerializableImpl.serialize(OMSerializableImpl.java:125) org.apache.axiom.om.impl.llom.OMSerializableImpl.serialize(OMSerializableImpl.java:113) org.apache.axiom.om.impl.llom.OMElementImpl.toString(OMElementImpl.java:988) java.lang.String.valueOf(String.java:2994) java.lang.StringBuffer.append(StringBuffer.java:265) org.apache.synapse.mediators.builtin.LogMediator.getFullLogMessage(LogMediator.java:203) org.apache.synapse.mediators.builtin.LogMediator.getLogMessage(LogMediator.java:138) org.apache.synapse.mediators.builtin.LogMediator.mediate(LogMediator.java:101) org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:97) org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:59) org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158) org.apache.synapse.rest.Resource.process(Resource.java:343) org.apache.synapse.rest.API.process(API.java:399) org.apache.synapse.rest.RESTRequestHandler.apiProcess(RESTRequestHandler.java:123) org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:101) org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:69) org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:304) org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:75) org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:144) org.apache.axis2.transport.http.util.RESTUtil.processXMLRequest(RESTUtil.java:89) org.apache.synapse.transport.nhttp.util.RESTUtil.processPOSTRequest(RESTUtil.java:213) org.apache.synapse.transport.nhttp.ServerWorker.processEntityEnclosingMethod(ServerWorker.java:468) org.apache.synapse.transport.nhttp.ServerWorker.run(ServerWorker.java:291) org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) java.lang.Thread.run(Thread.java:745)
注意:当我们使用通用 SOAP 代理和消息时,可以正常工作.字符集没问题.
Note: When we use common SOAP proxy and messages works fine. No problem with charset.
注意:就像@weibeld 说的,我的语言是葡萄牙语.
Note: Like @weibeld said, my language is Portuguese.
我的 REST API 有一个带有 POST 方法的资源,我正在向服务器发送以下请求:
My REST API has a resource with POST method and I am sending the following request to server:
POST http://myHost:8280/api/v1/myResource HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json;charset=utf-8
Content-Length: 741
Host: myHost:8280
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
{
"customerName": "CONSULTORIA EMPRESARIAL EIRELI - ME",
"serviceId": "05948517",
"productName": "Banda Larga Expansão Sul",
"localityCode": "51000",
"status": "OCUPADO",
"isChangeOfAddress": "FALSO",
"serial": "0047848517",
"vlanType": "1Q",
"ont_id": "0047848517",
"modelo_ont": "ZHONE-2425",
"accessType": "GPON",
"streetCode": "4535",
"neighbourhood": "NEIGHBOURHOOD",
"streetNumber": "4428",
"complement1": "2"
}
注意:在这一行 "productName": "Banda Larga Expansão Sul",
我有一个 ão
.
Note: In this line "productName": "Banda Larga Expansão Sul",
I have an ão
.
总而言之,我正在发送一个带有 application/json;charset=utf-8 的 POST 并且 wso2esb 引发了一个关于 ão
的错误,当我发送了一个 ISO-8859-1 工作正常.(当我使用带有 utf-8 的肥皂时可以正常工作).
Summarizing, I am sending a POST, wiht application/json;charset=utf-8 and the wso2esb is raising an error about the ão
, and when I sent an ISO-8859-1 works fine. (when I use soap with utf-8 works properly).
推荐答案
您的客户端发送到服务器的 HTTP 请求似乎是使用 ISO-8859-1 而不是 UTF-8 编码的.
It seems that the the HTTP request that your client sends to the server is encoded with ISO-8859-1 rather than with UTF-8.
服务器然后尝试使用 UTF-8 解码此请求.这适用于所有 ASCII 字符.但是,在某些时候,服务器会遇到您报告的非 ASCII 字符之一的 ISO 8859-1 代码:
The server then tries to decode this request with UTF-8. This works well for all ASCII characters. However, at some point the server encounters the ISO 8859-1 code of one of your reported non-ASCII characters:
- ã ==> 0xE3 = 11100011
- ç ==> 0xE7 = 11100111
- é ==> 0xE9 = 11101001
(将上面的列表读作:[字符] ==> [ISO 8859-1 代码十六进制.] = [ISO 8859-1 代码箱.])
现在 UTF-8 将此字节解释为 3 字节 UTF-8 代码的前导字节,因为它以 1110 开头(请参阅 此处).因此,UTF-8 期望下一个字节是 UTF-8 连续字节,它必须以 10 开头.然而,下一个字节是 0x6f,即 ASCII 字符 o
的 ISO 8859-1 代码, 二进制为 01101111,即不以 10 开头.
Now UTF-8 interprets this byte as the leading byte of a 3-byte UTF-8 code because it starts with 1110 (see here). Consequently, UTF-8 expects the next byte to be a UTF-8 continuation byte, which must start with 10. However, the next byte is 0x6f, the ISO 8859-1 code for the ASCII character o
, which is 01101111 in binary, i.e. does not start with 10.
因此出现错误消息Invalid UTF-8 middle byte 0x6f
.
所以,我猜您的请求包含子字符串 ão
、ço
或 éo
,如果您的语言是葡萄牙语,这很可能.
So, I guess that your request contains the substring ão
, ço
, or éo
, which is likely if your language is Portuguese.
总结:您需要确保客户端使用 UTF-8 编码请求.我不知道您是如何创建请求的,但应该可以在您使用的任何函数中设置编码.
In summary: you need to make sure that the client encodes the request with UTF-8. I don't know how you create the request, but there should be a possibility to set the encoding in whatever function you use.
这篇关于WSO2ESB Rest API 错误:无效的 UTF-8 中间字节 0x6f(在字符 #143,字节 #127)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!