WSO2,将空的 SOAP 响应转换为空的 JSON

WSO2, convert an empty SOAP response to an empty JSON

我有一个 EI 6.6.0 服务可以 return 空响应,但它们必须是 JSON,以防出现空响应

TID: [-1234] [] [2021-08-26 09:03:16,819]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:5be867bf-2210-4ccd-8ecd-97a6078500f8, Direction: response, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>`

转换后的响应应该是 {}

我正在使用 属性

<property name="messageType" value="application/json" scope="axis2"/>

管理转换,但出现错误

TID: [-1234] [] [2021-08-26 09:06:17,135] ERROR {org.apache.synapse.commons.json.JsonUtil} - #writeAsJson. Payload could not be written as JSON. MessageID: urn:uuid:28a3eea3-a226-483a-8d5d-68d359d0fc08

TID: [-1234] [] [2021-08-26 09:06:17,136] ERROR {org.wso2.carbon.integrator.core.json.JsonStreamFormatter} - Error occurred while writing to application/json java.lang.reflect.InvocationTargetException

如果我使用

<property name="NO_ENTITY_BODY" value="true" scope="axis2" type="BOOLEAN" />

那么转换不会失败,但是return没什么。

以下是我的输出流程:

<log level="full" xmlns="http://ws.apache.org/ns/synapse"/>
<switch source="//soapenv:Body/*[1]" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <case regex="">
      <property name="NO_ENTITY_BODY" value="true" scope="axis2" type="BOOLEAN" />
  </case>
  <default/>
</switch>
<property name="messageType" value="application/json" scope="axis2"/>
<respond/>

你可以试试PayloadFactory Mediator

<payloadFactory media-type="json">
  <format>{}</format>
  <args/>
</payloadFactory>