【问题标题】:Add new line to custom Exception message in Anypoint Studio Mule在 Anypoint Studio Mule 中为自定义异常消息添加新行
【发布时间】:2017-06-26 13:05:39
【问题描述】:

我正在使用 Anypoint Studio 6.1 和 Mule 3.8.1,并希望返回一条异常消息,其中“\n”作为新行返回。我正在尝试使用 System.getProperty("line.separator") 但“\n”仍然作为文本出现在消息中,而不是新行。

我怎样才能让它工作?

XML 测试流程:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <flow name="tempflowFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test-flow" allowedMethods="GET" doc:name="HTTP"/>
        <set-payload value="{
  &quot;Exception&quot;: {
    &quot;status&quot;: -1,
    &quot;description&quot;: &quot;\nJson content is not compliant with schema\ncom.github.fge.jsonschema.core.report.ListProcessingReport: failure\n--- BEGIN MESSAGES ---\nerror: object has too many properties (found 2 but schema requires at most 1)\n    level: \&quot;error\&quot;\n    schema: {\&quot;loadingURI\&quot;:\&quot;file:/C:/temp.json#\&quot;,\&quot;pointer\&quot;:\&quot;/properties/field1\&quot;}\n    instance: {\&quot;pointer\&quot;:\&quot;/field1\&quot;}\n    domain: \&quot;validation\&quot;\n    keyword: \&quot;maxProperties\&quot;\n    found: 2\n    required: 1\n---  END MESSAGES  ---\n (org.mule.module.json.validation.JsonSchemaValidationException).&quot;
  }
}" encoding="UTF-8" mimeType="application/json" doc:name="Set Payload"/>
        <logger message="#[payload.replace(&quot;\n&quot;, System.getProperty('line.separator'))]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[payload.replace(&quot;\n&quot;, System.getProperty('line.separator'))]" encoding="UTF-8" mimeType="application/json" doc:name="Set Payload"/>
    </flow>
</mule>

谢谢

【问题讨论】:

    标签: mule newline anypoint-studio getproperty


    【解决方案1】:

    看起来您需要转义换行符。此外,在 MEL 表达式中使用单引号 (') 而不是双引号 (")。

    <logger message="#[payload.replace('\\n', System.getProperty('line.separator'))]" level="INFO"
            doc:name="Logger" />
        <set-payload value="#[payload.replace('\\n', System.getProperty('line.separator'))]" encoding="UTF-8"
            mimeType="application/json" doc:name="Set Payload" />
    

    上面的代码似乎可以工作:

    INFO  2017-02-09 18:03:09,349 [Mule.app.deployer.monitor.1.thread.1] org.mule.module.launcher.MuleDeploymentService: 
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    + Started app 'test2'                                      +
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    INFO  2017-02-09 18:03:17,280 [[test2].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: {   "Exception": {     "status": -1,     "description": "
    Json content is not compliant with schema
    com.github.fge.jsonschema.core.report.ListProcessingReport: failure
    --- BEGIN MESSAGES ---
    error: object has too many properties (found 2 but schema requires at most 1)
        level: \"error\"
        schema: {\"loadingURI\":\"file:/C:/temp.json#\",\"pointer\":\"/properties/field1\"}
        instance: {\"pointer\":\"/field1\"}
        domain: \"validation\"
        keyword: \"maxProperties\"
        found: 2
        required: 1
    ---  END MESSAGES  ---
    (org.mule.module.json.validation.JsonSchemaValidationException)."   } }
    

    Mule log

    【讨论】:

    • 谢谢。当我试图逃避它时,我一定有什么问题。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多