WSO2 API Manager将JSON转换为XML(并且失败了请求)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WSO2 API Manager将JSON转换为XML(并且失败了请求)相关的知识,希望对你有一定的参考价值。
我们使用WSO2 API管理器作为API网关。我拥有的API之一是仅支持JSON的非完全安全的API。它使用带有JSON的HTTP POST请求作为数据交换格式。我有一个非常基本的配置:配置为允许POST / *的REST API。
一些响应包括JSON,它具有像"P~A"
这样的字符串作为对象键。当我尝试通过网关调用它时,它会响应响应:
2018-09-13 18:37:37,890 [-] [PassThroughMessageProcessor-36] ERROR JsonUtil #writeAsJson. Could not convert OMElement to JSON. Invalid XML payload. Error>>> Unexpected character '~' (code 126) excepted space, or '>' or "/>"
at [row,col {unknown-source}]: [1,4956]
2018-09-13 18:37:37,891 [-] [PassThroughMessageProcessor-36] ERROR PassThroughHttpSSLSender Failed to submit the response
org.apache.axis2.AxisFault: Could not convert OMElement to JSON. Invalid XML payload.
at org.apache.synapse.commons.json.JsonUtil.writeAsJson(JsonUtil.java:372)
at org.apache.synapse.commons.json.JsonFormatter.writeTo(JsonFormatter.java:84)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:573)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:264)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.synapse.core.axis2.Axis2Sender.sendBack(Axis2Sender.java:230)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send(Axis2SynapseEnvironment.java:531)
at org.apache.synapse.mediators.builtin.SendMediator.mediate(SendMediator.java:118)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:97)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:59)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.rest.Resource.process(Resource.java:343)
at org.apache.synapse.rest.API.process(API.java:338)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:90)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:56)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:304)
at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:554)
at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:188)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:262)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '~' (code 126) excepted space, or '>' or "/>"
at [row,col {unknown-source}]: [1,4956]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:647)
at com.ctc.wstx.sr.BasicStreamReader.handleNsAttrs(BasicStreamReader.java:2996)
at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2963)
at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2839)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1073)
at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
at org.codehaus.stax2.ri.Stax2ReaderAdapter.next(Stax2ReaderAdapter.java:129)
at org.codehaus.stax2.ri.Stax2EventReaderImpl.peek(Stax2EventReaderImpl.java:367)
at org.apache.synapse.commons.staxon.core.event.SimpleXMLEventWriter.add(SimpleXMLEventWriter.java:118)
at org.apache.synapse.commons.json.JsonUtil.writeAsJson(JsonUtil.java:368)
... 23 more
2018-09-13 18:37:37,892 [-] [PassThroughMessageProcessor-36] ERROR Axis2Sender Access-Control-Allow-Headers:authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,Access-Control-Allow-Methods:HEAD,DELETE,POST,PATCH,PUT,GET,Access-Control-Allow-Origin:*,Content-Type:application/json;charset=utf-8,<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject>
(然后它继续并包含XML,它试图使用来自JSON的"P~A"
密钥作为<P~A>
XML标签,这就是它的窒息)
API定义未启用消息中介,并且还禁用了响应缓存。所以,我不清楚为什么它甚至使用中介并尝试将我的JSON响应转换为XML。
那么,问题是:如何使这个工作?通过配置它来应对将其转换为XML或不需要?
我还要注意,这个过程需要很长时间。我得到(错误)响应需要很长时间,但我发现响应数据很快就会到达API管理器。
我们还运行WSO2 Analytics服务。我试图阻止它,以防它导致问题。没有帮助
我发布这个作为答案,因为它解决了这个问题。加
synapse.commons.json.buildValidNCNames=true
到repository/conf/synapse.properties
文件。
它使用字符代码处理~
(以及标签中无效的任何其他内容)。
我想也许调解不是消息调解(被禁用),而是更内部的调解。在那种情况下,我真的不明白为什么上面的设置不是默认值 - 我没有看到它的缺点,没有它,网关无法应付某些JSON!
以上是关于WSO2 API Manager将JSON转换为XML(并且失败了请求)的主要内容,如果未能解决你的问题,请参考以下文章
WSO2 API Manager 拒绝设置不安全的标头“Cookie”
WSO2 Api Manager公共本地移动应用中的OAuth2 DCR安全性
WSO2 ESB 无法将完整的 JSON 数据转换为 XML