我的目标非常简单。我只想将来自 Mule 中 Jersey 模块的 json 响应包装在 JSON 响应中,这样:
{"status": "ok", message:<json-message>}
我尝试使用自定义变压器:
<flow name="sicsRest" doc:name="sicsRest">
<inbound-endpoint address="http://localhost:65432/RestServices"
exchange-pattern="request-response" doc:name="RestEntry" />
<jersey:resources doc:name="REST">
<component doc:name="Java">
<spring-object bean="serviceImpl" />
</component>
<custom-transformer returnClass="java.lang.String"
class="com.hyperborea.sicsbus.transformer.JsonTransformer"
doc:name="JsonTransformer" />
<jersey:exception-mapper
class="com.hyperborea.sicsbus.exception.JsonRestExceptionMapper" />
</jersey:resources>
</flow>
变压器类:
public class JsonTransformer extends AbstractTransformer {
@Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
return "{\"status\":\"ok\", \"message\":\"" + src + "\"}";
}
}
不幸的是,它在 Mule 启动期间引发了异常:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'custom-transformer'. One of '{"http://www.mulesoft.org/schema/mule/core":abstract-component, "http://www.mulesoft.org/schema/mule/jersey":exception-mapper}' is expected.
有什么建议吗?