Reputation: 21
i am trying to use jenkins pipeline to show API payload, on the ApiUrl1 runs normally without problems, but on the APiURL2 show that error is Error occurred: java.io.NotSerializableException: groovy.json.internal.LazyMap
try{
final String response = sh(script: """curl -svf --globoff --request GET --header "Authorization: Bearer ${token}" "$apiUrl" """, returnStdout: true).trim()
// Parse API response
def jsonSlurper = new groovy.json.JsonSlurper()
def result = jsonSlurper.parseText(response)
String testingResponse = result.toString()
echo testingResponse
def payload = result.data
for (def jobTitleList in payload) {
def jobTitleName = jobTitleList.job_title_id.job_title
jobTitles << jobTitleName
}
//def jobTitles = payload.collect { it.job_title_id.job_title }
echo "${jobTitles}"
for (def jobTitleName in jobTitles) {
String jobTitleString = jobTitleName.toString()
echo "Job Title : ${jobTitleString}"
echo apiUrl2
try {
echo "Line 87"
// Execute curl command to fetch data
final String response2 = sh(script: """https_proxy=http://{proxy}curl -svf --globoff --request GET --header "Authorization: Bearer ${token}" "${apiUrl2}" """, returnStdout: true).trim()
echo "Line 91"
// Parse API response
def result2 = jsonSlurper.parseText(response2)
String testingResponse2 = result2.toString()
echo testingResponse2
} catch (Exception e) {
echo "Error encountered while fetching data"
echo "Error message: ${e.message}"
}
}
} catch(e) {
echo "${e.message}"
echo "No PIC"
}
return jobTitles
}
Upvotes: 1
Views: 601
Reputation: 1
If you run your API call inside a function, and use the below method to convert back to a HashMap
before returning the API response back to the global scope, this will resolve the issue. This function recursively converts all LazyMap
's within the payload.
def convertLazyMap(obj) {
if (obj instanceof Map) {
def serializableMap = [:]
obj.each { key, value ->
serializableMap[key] = convertLazyMap(value)
}
return serializableMap
} else if (obj instanceof List) {
return obj.collect { convertLazyMap(it) }
} else {
return obj
}
}
Upvotes: 0
Reputation: 646
Your problem is holding non-serializable objects in the scope for too long
The JsonSlurper
class in Groovy is not serializable.
Jenkins tries to serialize objects periodically, so you randomly get NotSerializableException: groovy.json.internal.LazyMap
See this post for more details: Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
Upvotes: 0