使用JsonBuilder的toPrettyString时出现StackOverflowError

时间:2019-07-02 12:04:38

标签: jenkins groovy httprequest

我有以下代码。 它接收Jenkins工作区中本地可用的文件名,读取文件,然后尝试将其内容发送到端点。

对于service-configuration.json,此方法有效。但是,对于其余文件,它将失败并显示StackOverflowError ...

// Update Service configuration.
serviceJson = readJSON file: "service-configuration.json"

response = httpRequest validResponseCodes: "100:504", acceptType: "APPLICATION_JSON", contentType: "APPLICATION_JSON", customHeaders: [[name: 'Authorization', value: iamToken]], httpMode: "PUT", requestBody: new JsonBuilder(serviceJson).toPrettyString(), url: GlobalCatalog.service
if (response.status != 200) {
  println "Failure: ${response.status} ${response.content}"
  currentBuild.result = 'FAILURE'
  error "Failed uploading Global Catalog entry."
}

// Update Plans and their Deployments configurations.
GlobalCatalog.plans.each { plan ->
  planJson = readJSON file: "${plan.planName}-plan-configuration.json"

  response = httpRequest validResponseCodes: "100:504", acceptType: "APPLICATION_JSON", contentType: "APPLICATION_JSON", customHeaders: [[name: 'Authorization', value: iamToken]], httpMode: "PUT", requestBody: new JsonBuilder(planJson).toPrettyString(), url: plan.url
  if (response.status != 200) {
    println "Failure: ${response.status} ${response.content}"
    currentBuild.result = 'FAILURE'
    error "Failed uploading Global Catalog entry."
  } else {
    plan.deployments.each { deployment ->
      deploymentJson = readJSON file: "${plan.planName}-${deployment.region}-deployment-configuration.json"

      response = httpRequest validResponseCodes: "100:504", acceptType: "APPLICATION_JSON", contentType: "APPLICATION_JSON", customHeaders: [[name: 'Authorization', value: iamToken]], httpMode: "PUT", requestBody: new JsonBuilder(deploymentJson).toPrettyString(), url: deployment.url    
      if (response.status != 200) {
        println "Failure: ${response.status} ${response.content}"
        currentBuild.result = 'FAILURE'
        error "Failed uploading Global Catalog entry."
      }
    }
  }
}

StackOverflowError:

java.lang.StackOverflowError
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)
    at groovy.json.JsonOutput.writeObject(JsonOutput.java:321)
    at groovy.json.JsonOutput.writeMap(JsonOutput.java:458)

编辑:如果我改用toString(),则可以正常工作-但文件中各种语言字符串的编码不会保留...

1 个答案:

答案 0 :(得分:0)

正如评论中所建议的那样,使用JsonOutput.prettyPrint(myProp)的效果要好于new JsonBuilder(myProp).toPrettyString(),尽管我不确定为什么...