我有一个安装了 DSS 功能的 WSO2 ESB 服务器。当我在请求标头中使用“accept: application/json”调用 REST 服务时,我没有收到 JSON 响应,只收到 XML。
在此 API 中,为了获取 JSON 格式的响应,我使用这两个属性:
<property name="Accept" value="application/json" scope="transport" type="STRING"/>
<property name="messageType" value="application/json" scope="axis2"/>
您可以在此处查看实现
<api xmlns="http://ws.apache.org/ns/synapse" name="showById" context="/getById">
<resource methods="POST GET" uri-template="/getEmployeeXML/{Id}">
<inSequence>
<property name="getId" expression="get-property('uri.var.Id')" scope="default" type="STRING"/>
<log>
<property name="ID" expression="get-property('getId')"/>
</log>
<property name="Accept" value="application/json" scope="transport" type="STRING"/>
<property name="messageType" value="application/json" scope="axis2"/>
<payloadFactory media-type="xml">
<format>
<p:getEmployeeXML xmlns:p="arce.farmacy">
<xs:Id xmlns:xs="arce.farmacy">$1</xs:Id>
</p:getEmployeeXML>
</format>
<args>
<arg evaluator="xml" expression="get-property('getId')"/>
</args>
</payloadFactory>
<property name="Action" value="urn:getEmployeeXML" scope="transport" type="STRING"/>
<property name="SOAPAction" value="urn:getEmployeeXML" scope="transport" type="STRING"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<send>
<endpoint>
<address uri="http://169.254.193.10:9763/services/StoredProcedureFarmacy" format="soap11"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
<faultSequence>
<sequence key="{faultSEQ}"/>
</faultSequence>
</resource>
</api>
这是回复
{
"Employees": {
"Employe": [
{
"Id": 5,
"Description": "Office 2"
}
]
}
}