我的目标很愚蠢。 我只想在Jule响应中将来自Mule的Jersey模块的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.
有什么建议吗?
答案 0 :(得分:2)
如果您将custom-transformer
放在<response> ... </response>
和inbound-endpoint
之间的jersey:resources
块中,该怎么办?
新流程:
<flow name="sicsRest" doc:name="sicsRest">
<inbound-endpoint ... />
<response>
<custom-transformer returnClass="java.lang.String"
class="com.hyperborea.sicsbus.transformer.JsonTransformer"
doc:name="JsonTransformer" />
</response>
<jersey:resources doc:name="REST">
<component doc:name="Java">
<spring-object bean="serviceImpl" />
</component>
<jersey:exception-mapper ... />
</jersey:resources>
</flow>
必须在AbstractMessageTransformer中更改变换器才能更好地处理输入(使用AbstractTransformer几乎无法实现):
public class JsonTransformer extends AbstractMessageTransformer {
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
return "{\"status\":\"ok\", \"message.getPayloadAsString()\":\""
+ src + "\"}";
} catch (Exception e) {}
}
}