HTTPBuilder 将 valuecount、strings、bytes 添加到请求正文中,而对方无法解析该 JSON

问题描述

我需要使用 groovy HTTPBuilder 发送请求,这里是代码

String authToken = "token"
def cfManager = ComponentAccessor.getCustomFieldManager()
def addressJira = "http://xxx"
def http = new HTTPBuilder("${address}")
    
    http.setClient(HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build())
    try {
        http.request(POST,ContentType.JSON) {            
            headers.'Authorization' = "token ${authToken}"
            headers.'Content-Type' = 'application/json'           
            headers.'cache-control' = 'no-cache'            
  body = [
          "request": [
           "token":"${authToken}","commonData":[
              "taskId":"${issue.id.toString()}"
           ],"phonesData":[
             "phone":"${cfManager.getCustomFieldobject("customfield_xxx")?.getValue(issue)?.toString()}","phoneType": 1
            ],"addData": [
              "title":"${issue.summary.toString()}","description":"${issue.description.toString()}"
             ]
          ]
         ]   
  requestContentType = ContentType.JSON
            
  response.success = { resp,json ->
   log.info("Resp status  " + resp.status.toString())
   log.info("JSON ${json.toString()}")
   log.info("JSON text ${json.text}")
  }

   response.failure = { resp ->
    log.warn("response code is : " + resp.status.toString())
    log.warn("response is : " + resp.getEntity().getContent().getText())
   }
       }
    } catch (Exception e) {
        log.warn("Exceptiion while request" + e)
    }

我得到 resp 代码 200 但另一方面他们得到这样的 JSON 并且由于“valueCount”、“strings”和“bytes”而无法解析它:

{
    "request": {
        "token": {
            "valueCount": 1,"strings": ["",""],"bytes": [85,69,108,112,120,90,99,113,107,100,106,71,121,102,199,115,103,33,45,109,70,37,65,91,47,77,54,83,111,49,50,74,122],"values": ["token"]
        },"commonData": {
            "taskId": {
                "valueCount": 1,"bytes": [48,55,56,56],"values": ["here is issue id"]
            }
        },"phonesData": {
            "phone": {
                "valueCount": 1,"bytes": [43,53,57,48],"values": ["+01234567890"]
            },"phoneType": 1
        },"addData": {
            "title": {
                "valueCount": 1,"bytes": [84,101,116],"values": ["Test"]
            },"description": {
                "valueCount": 1,"bytes": [-113,-48,-78,-70,-80,32,-76,-69,-47,-113,-102,-90,-98,-73,-66,-67,46],"values": ["here is issue summary"]
            }
        }
    }
}

所以问题是为什么他们会得到这些参数,我必须做什么才能不发送“valueCount”、“strings”和“bytes”? 感谢任何帮助

解决方法

某些版本的 Camel 中的序列化程序无法识别 GString 类型。 因此,您需要通过调用 toString() 来强制转换为字符串

def v_stringvar = "${var1} text ${var2}";

然后你就这样用了

v_stringvar?.toString()