WSO2 ESB代理服务在传递给jaggery js时转义JSON有效负载

时间:2013-06-18 03:57:45

标签: wso2 wso2esb wso2carbon jaggery-js

我正在使用JSON有效负载发布到ESB(4.6.0)代理服务。这是一个传递代理服务,路由到一个粗糙的JS应用程序。 jaggery应用程序试图从有效负载中访问JSON,但它无法进行,因为JSON字符串被转义。

以下是请求标头(来自网络 - > ESB):

POST /services/testJaggPS HTTP/1.1
Host: pb-dvwa8:7280
Content-Length: 29
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

{
  "PARAM": "hello world" 
}

以下是来自jaggery app的JSON响应:

{"RESPONSE" : {"test" : "hi", "PARAM" : null, "content" : "{\n \"PARAM\" : \"HELLO WORLD\" \n}"}}

请注意,“内容”(存储来自网络的JSON有效负载的变量)正在转义。

我已经研究了消息构建器/格式化程序,但它还没有解决这个问题。这是我当前的代理服务(可能不需要所有的消息构建器):

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testJaggPS"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <target endpoint="jaggRouter">
      <inSequence>
         <builder>
            <messageBuilder contentType="application/xml"
                            class="org.apache.axis2.builder.ApplicationXMLBuilder"
                            formatterClass="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>
            <messageBuilder contentType="application/xop+xml"
                            class="org.apache.axis2.builder.MTOMBuilder"/>
            <messageBuilder contentType="multipart/related"
                            class="org.apache.axis2.builder.MIMEBuilder"/>
            <messageBuilder contentType="application/json"
                            class="org.apache.axis2.json.JSONBuilder"
                            formatterClass="org.apache.axis2.json.JSONMessageFormatter"/>
            <messageBuilder contentType="application/x-www-form-urlencoded"
                            class="org.apache.axis2.builder.XFormURLEncodedBuilder"
                            formatterClass="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
            <messageBuilder contentType="text/xml"
                            class="org.apache.axis2.builder.SOAPBuilder"
                            formatterClass="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
            <messageBuilder contentType="application/soap+xml"
                            class="org.apache.axis2.builder.SOAPBuilder"
                            formatterClass="org.apache.axis2.transport.http.SOAPMessageFormatter"/>
            <messageBuilder contentType="application/json/badgerfish"
                            class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
         </builder>
         <property name="messageType" value="application/json" scope="axis2"/>
         <property name="contentType" value="application/json" scope="axis2"/>
      </inSequence>
      <outSequence>
         <property name="messageType" value="application/json" scope="axis2"/>
         <property name="contentType" value="application/json" scope="axis2"/>
         <send/>
      </outSequence>
   </target>
</proxy>

这是我的粗糙代码:

<%
// load core files
response.contentType = 'application/json';

var url = request.getRequestURL();
var theContent = request.getContent();  
 // note null 'PARAM' and escaped 'content'    
response.content = {"RESPONSE" : {
  "test" : "hi",
    "PARAM" : theContent.PARAM,
    "content" : theContent
}};

%>

1 个答案:

答案 0 :(得分:0)

当您在Content-Type标头中有其他内容(例如字符编码等)时,Jaggery的自动JSON解析存在问题。它已在主分支中修复。

但是,如果您尝试使用早期版本,则必须使用 JSON.parse()手动解析内容。即替换你的

var theContent = request.getContent();

在Jaggery文件中,用

var theContent = JSON.parse(request.getContent());