6

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

我的骡子配置文件是:

<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. 我应该怎么办?

4

1 回答 1

9

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

<response>
  <message-properties-transformer>
    <add-message-property key="Content-Type" value="application/json" />
  </message-properties-transformer>
</response>
于 2011-08-04T15:58:06.773 回答