json - 如何在 WSO2 Enterprise Integrator 中处理 Json Formatters
问题描述
我有一个问题,这让我发疯。
场景:
WSO2 EI 6.4.0
Oracle 数据库 12c。
我有一个数据服务 (ViculoDataService) 在 Oracle 数据库的特定表内提供“搜索”能力。
当我从 SOAPUI 调用 VinculoDataService 时,我收到了正确的 json 答案:
VinculoDataService RESPONSE FOR application/json:
{"vinculos": {"vinculo": [{
"relacaoDependencia": {
"codigo": null,
"descricao": null
},
"produto": {
"registroPlanoRPS": "410315997",
"cadastroPlanoSCPA": null,
"status": "Inativo"
},
"contrato": {
"codigo": "6",
"numeroContratoApolice": "APO-006"
},
"dataAdesaoPlano": "2018-12-31T19:00:00.000-05:00",
"cco": "100000001705",
"titular": {"pessoaFisica": {
"cpf": null,
"cns": null,
"dataNascimento": null
}},
"status": "Ativo",
"beneficiario": {"pessoaFisica": {
"cpf": "06425417609",
"cns": null,
"dataNascimento": "1978-04-24T19:00:00.000-05:00"
}}
}]}}
The problem occurs when I try to call it from a sequence mediation as show above.
myAPI.xml:
<inSequence>
<property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
<header name="Accept" scope="transport" value="application/json"/>
<call blocking="true">
<endpoint>
<http method="get" uri-template="http://wso2ei.coreconsulting:8282/services/VinculoDataService/pesquisar?operadora={uri.var.operadora}&contrato={uri.var.codigo}"/>
</endpoint>
</call>
这是给我的第一个问题错误响应:
Error log message for wso2carbon.xml:
>> "GET /services/VinculoDataService/pesquisar?operadora=313149&contrato=6 HTTP/1.1[\r][\n]"
[2019-11-26 14:10:05,608] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "Content-Type: application/json; charset=UTF-8[\r][\n]"
[2019-11-26 14:10:05,608] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "Accept: application/json[\r][\n]"
[2019-11-26 14:10:05,608] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2019-11-26 14:10:05,609] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "User-Agent: Axis2[\r][\n]"
[2019-11-26 14:10:05,609] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "Host: wso2ei.coreconsulting:8282[\r][\n]"
[2019-11-26 14:10:05,609] [-1234] [] [PassThroughMessageProcessor-37] DEBUG {httpclient.wire.header} - >> "[\r][\n]"
[2019-11-26 14:10:05,781] [-1234] [] [PassThroughMessageProcessor-38] ERROR {org.wso2.carbon.integrator.core.json.JsonStreamFormatter} - Error occurred while writing to application/json
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor69.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.wso2.carbon.integrator.core.json.JsonStreamFormatter.writeTo(JsonStreamFormatter.java:107)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:614)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:280)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver.invokeBusinessLogic(AbstractInOutSyncMessageReceiver.java:45)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:337)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:158)
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: java.lang.NullPointerException: charsetName
at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:99)
at org.apache.axis2.json.gson.JsonFormatter.writeTo(JsonFormatter.java:61)
... 16 more
[2019-11-26 14:10:05,785] [-1234] [] [PassThroughMessageProcessor-38] ERROR {org.apache.synapse.transport.passthru.PassThroughHttpSender} - Failed to submit the response
org.apache.axis2.AxisFault
at org.wso2.carbon.integrator.core.json.JsonStreamFormatter.writeTo(JsonStreamFormatter.java:113)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:614)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:280)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver.invokeBusinessLogic(AbstractInOutSyncMessageReceiver.java:45)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:337)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:158)
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)
[2019-11-26 14:10:05,786] [-1234] [] [PassThroughMessageProcessor-38] ERROR {org.apache.synapse.transport.passthru.ServerWorker} - Error processing GET request for : /services/VinculoDataService/pesquisar?operadora=313149&contrato=6
org.apache.axis2.AxisFault: Failed to submit the response
at org.apache.synapse.transport.passthru.PassThroughHttpSender.handleException(PassThroughHttpSender.java:676)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:282)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver.invokeBusinessLogic(AbstractInOutSyncMessageReceiver.java:45)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:337)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:158)
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: org.apache.axis2.AxisFault
at org.wso2.carbon.integrator.core.json.JsonStreamFormatter.writeTo(JsonStreamFormatter.java:113)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:614)
at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:280)
经过一番研究,我找到了这个答案:WSO2 EI: Dataservice usinggiving INCOMPATIBLE_PARAMETERS_ERROR when calling through REST API并遵循 wso2 EI 参考指南https://docs.wso2.com/display/EI640/Working+with+Message+Builders+and +格式化程序。
通过这些参考,我尝试为 json(文本/json 和文本/javascript)配置其他消息生成器和格式化程序,如下所示:
(axis2.xml FILE)
<messageFormatter contentType="application/json"
class="org.wso2.carbon.integrator.core.json.JsonStreamFormatter"/>
<messageFormatter contentType="text/javascript"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/>
<messageFormatter contentType="text/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>
...
<messageBuilder contentType="application/json"
class="org.wso2.carbon.integrator.core.json.JsonStreamBuilder"/>
<messageBuilder contentType="text/javascript"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
<messageBuilder contentType="text/json"
class="org.apache.axis2.json.JSONBuilder"/>
我已经从 SOAPUI 对 bove(text/javascritp 和 text/json)进行了测试,两者都给了我几乎没有差异的响应。但是如果一个get it里面调解OK。
以下 json 显示媒体类型“text/json”和“text/javascrtipt”的答案
{"vinculos": {"vinculo": {
"relacaoDependencia": {
"codigo": {"@nil": "true"},
"descricao": {"@nil": "true"}
},
"produto": {
"registroPlanoRPS": "410315997",
"cadastroPlanoSCPA": {"@nil": "true"},
"status": "Inativo"
},
"contrato": {
"codigo": "6",
"numeroContratoApolice": "APO-006"
},
"dataAdesaoPlano": "2018-12-31T19:00:00.000-05:00",
"cco": "100000001705",
"titular": {"pessoaFisica": {
"cpf": {"@nil": "true"},
"cns": {"@nil": "true"},
"dataNascimento": {"@nil": "true"}
}},
"status": "Ativo",
"beneficiario": {"pessoaFisica": {
"cpf": "06425417609",
"cns": {"@nil": "true"},
"dataNascimento": "1978-04-24T19:00:00.000-05:00"
}}
}}}
问题是使用那些新的媒体类型,我在调解中收到无效响应:
Wso2carbon.log Error message using 'text/json' or 'text/javascritp' media types:
"GET /services/VinculoDataService/pesquisar?operadora=313149&contrato=6 HTTP/1.1[\r][\n]"
[2019-11-26 13:54:52,443] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "Content-Type: application/json; charset=UTF-8[\r][\n]"
[2019-11-26 13:54:52,443] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "Accept: text/json[\r][\n]"
[2019-11-26 13:54:52,443] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2019-11-26 13:54:52,443] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "User-Agent: Axis2[\r][\n]"
[2019-11-26 13:54:52,443] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "Host: wso2ei.coreconsulting:8282[\r][\n]"
[2019-11-26 13:54:52,444] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - >> "[\r][\n]"
[2019-11-26 13:54:52,652] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "HTTP/1.1 200 OK[\r][\n]"
[2019-11-26 13:54:52,652] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "HTTP/1.1 200 OK[\r][\n]"
[2019-11-26 13:54:52,653] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "Content-Type: text/json; charset=UTF-8[\r][\n]"
[2019-11-26 13:54:52,653] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "Date: Tue, 26 Nov 2019 18:54:52 GMT[\r][\n]"
[2019-11-26 13:54:52,653] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "Transfer-Encoding: chunked[\r][\n]"
[2019-11-26 13:54:52,653] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.header} - << "[\r][\n]"
[2019-11-26 13:54:52,655] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.content} - << "f"
[2019-11-26 13:54:52,655] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.content} - << "[\r]"
[2019-11-26 13:54:52,655] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.content} - << "[\n]"
[2019-11-26 13:54:52,655] [-1234] [] [PassThroughMessageProcessor-34] DEBUG {httpclient.wire.content} - << "{"vinculos":""}"
[2019-11-26 13:54:52,657] [-1234] [] [PassThroughMessageProcessor-34] INFO {org.apache.axis2.builder.BuilderUtil} - OMException in getSOAPBuilder
org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
我们可以在最后一条日志中看到,中介内的 VinculoDataService 调用得到一个无效消息(“{“vinculos”:“”}”)。
那么我做错了什么?我不明白...
问候马塞洛。
解决方案
推荐阅读
- c# - C# WPF 将 UTF16 字符串传递给接受 char * 的函数
- python - 在python中将分层数据转换为特定的json格式
- javascript - 如何使用Javascript在句子中的单词之间添加动态值?
- sql - 无法使用 Group by - 显示错误:列必须出现在 GROUP BY 子句中或用于聚合函数
- python - 在 ACR1252U NFC 读卡器上强制 PC/SC 驱动程序并使用 python 进入卡仿真模式
- java - namenode 无法在 ambari 中启动
- nutch - 确保 Nutch 已爬取特定域的所有页面
- php - 在 IndexController.php 中执行 CLI 命令的问题
- sql - 从 MDX 查询 SSAS 返回两个日期之间的数据
- xpath - 如果在单个报告中使用两个 XPath 表达式,如何修复“未找到字段”错误?