【问题标题】:How to set content type = "application/json" in mule.?如何在 mule 中设置 content type = "application/json"。?
【发布时间】:2020-03-24 21:25:27
【问题描述】:

我有一个 HTML 文件,它由一个表单组成,它从用户那里获取某些参数,当 URL 被点击时,一个 Mule 服务(用 Java 编写)被调用并返回一个 JSON 字符串。现在如何在 Mule 或 Java 中设置 Content-type = application/json,以便浏览器知道它正在获取 JSON?

我的 Mule 配置文件是:

<custom-transformer name="HttpParams" class="org.mule.transport.http.transformers.HttpRequestBodyToParamMap" />
<message-properties-transformer name="HttpResponse">
    <add-message-property key="Content-Type" value="application/json" />
    <add-message-property key="http.status" value="303" />
</message-properties-transformer>

<flow name="jcars">
    <http:inbound-endpoint address="http://localhost:11221/jcars" transformer-refs="HttpParams" responseTransformer-refs="HttpResponse">
        <not-filter>
           <wildcard-filter pattern="/favicon.ico"/>
        </not-filter>
    </http:inbound-endpoint>

    <component class="defaults.basicapp.jcars"/>
</flow>

而我的 Java 类是 (Jcars):

public String onEvent(Object obj){

     String json = "{{"id":0,"model":"suzuki","make":"2002","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2003","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2004","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2005","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2006","ps":true,"price":101.2,"bhp":12.6}}";

     return json;
}

浏览器按原样显示字符串。它不知道内容类型是application/json。我该怎么办?

【问题讨论】:

    标签: content-type mule


    【解决方案1】:

    在您的流程中添加响应消息属性转换器以设置内容类型:

    <response>
      <message-properties-transformer>
        <add-message-property key="Content-Type" value="application/json" />
      </message-properties-transformer>
    </response>
    

    【讨论】:

    • 在这种情况下,您不能设置此属性:
    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2012-05-22
    • 1970-01-01
    • 2018-03-11
    • 2016-10-02
    • 2022-01-01
    相关资源
    最近更新 更多