gpt4 book ai didi

wso2 - 如何转换(JSON 到 XML)请求和响应(XML 到 JSON)以调用外部 Soap 服务

转载 作者:行者123 更新时间:2023-12-05 07:59:08 25 4
gpt4 key购买 nike

我是wso2的新手..我的要求是

1) 我想将传入的 JSON 请求转换为 XML 格式

2) 将该 XML 请求发送到外部 SOAP 服务

3) 响应将以 XML 格式获取,需要将其转换为 JSON 格式

有人这样做过吗?如果是这样,您能分享一下您是怎么做到的吗?

我有以下代理服务配置

`<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="CelsiusToFahrenheitService"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="messageType" value="text/xml" scope="axis2"/>
<property name="Proxy-Authorization"
expression="fn:concat('Basic', base64Encode('INDIA\username:pwd'))"
scope="transport"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
<send>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx"
format="soap11"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
</target>
<description/>
</proxy>`

XML 请求

<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
<Celsius>20</Celsius>
</CelsiusToFahrenheit>

XMl 响应

<CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/">
<CelsiusToFahrenheitResult>68</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>

需要将 REQUEST 作为 JSON 发送,RESPOSNE 也需要作为 JSON 获取

谁能帮我解决这个问题

如何使用ScriptMediator来做上面的代理服务。我从这里得到了样本 ( https://docs.wso2.org/display/ESB481/Sample+441%3A+Converting+JSON+to+XML+Using+JavaScript )

我是这样配置的(不知道对不对)

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<script language="js">var cel= mc.getPayloadJSON().CelsiusToFahrenheit.Celsius.toString();
mc.setPayloadXML(
&lt;CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"&gt;
&lt;Celsius&gt;{cel}&lt;/Celsius&gt;
&lt;/CelsiusToFahrenheit&gt;);</script>
<property name="messageType" value="text/xml" scope="axis2"/>
<log level="full"/>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx"
format="soap11"/>
</endpoint>
</target>
<description/>
</proxy>

但是我遇到了这样的异常

`[2014-03-20 18:19:02,391]  INFO - LogMediator To: /services/JsonToXMLProxy.JsonToXMLProxyHttpEndpoint, MessageID: urn:uuid:a2eeeb26-94e1-4ed1-a3f9-79f1d1461821, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"><Celsius>20</Celsius></CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>
[2014-03-20 18:19:03,130] WARN - ClientHandler Received an unexpected response - of content type : text/html and status code : 411 with reason : Length Required For : 172.26.40.214:8080 For Request : Axis2Request [Message ID : urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d] [Status Completed : true] [Status SendingCompleted : true]
[2014-03-20 18:19:04,133] INFO - BuilderUtil OMException in getSOAPBuilder
org.apache.axiom.om.OMException: SOAP message MUST NOT contain a Document Type Declaration(DTD)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createDTD(StAXSOAPModelBuilder.java:462)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:282)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:204)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:154)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:140)
at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(BuilderUtil.java:659)
at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:198)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)
at org.apache.synapse.transport.nhttp.ClientWorker.run(ClientWorker.java:253)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
[2014-03-20 18:19:04,137] INFO - BuilderUtil Remaining input stream :[]
[2014-03-20 18:19:04,137] WARN - ClientWorker Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)
[2014-03-20 18:19:04,153] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d, Direction: response, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>411</faultcode><faultstring>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</faultstring><detail>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>`

请帮我解决这个问题

提前致谢。

最佳答案

您可以使用此示例使用 de payloadfactory 中介来构建消息,它在您的场景中对我有用:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https http local"
startOnLoad="true"
trace="disable">
<description/>
<target>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/>
</endpoint>
<inSequence>
<log>
<property name="TEMPERATURA_ENTRADA" expression="json-eval($.celsius)"/>
</log>
<payloadFactory media-type="xml">
<format>
<web:CelsiusToFahrenheit xmlns:web="http://www.w3schools.com/webservices/">
<web:Celsius>$1</web:Celsius>
</web:CelsiusToFahrenheit>
</format>
<args>
<arg evaluator="json" expression="$.celsius"/>
</args>
</payloadFactory>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
</inSequence>
<outSequence>
<log>
<property xmlns:p="http://www.w3schools.com/webservices/"
name="TEMPERATURA_SALIDA"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</log>
<payloadFactory media-type="json">
<format>
"Temperatura" : {
"EnFahrenheit" : $1
}
</format>
<args>
<arg xmlns:p="http://www.w3schools.com/webservices/"
evaluator="xml"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</target>
</proxy>

关于wso2 - 如何转换(JSON 到 XML)请求和响应(XML 到 JSON)以调用外部 Soap 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com