Hi,
I have created a REST endpoint with POST method. The client will call the endpoint with input JSON. I still can't figure out how to parse and construct a generic JSON object from the request body in order to retrieve the input values.
Please enlighten and thanks.
In what applications are you trying to construct and parse a generic JSON?
I tried the sample from https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html to create a REST endpoint:
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls
import com.atlassian.jira.issue.fields.rest.json.beans.PriorityJsonBean
import com.atlassian.jira.issue.priority.Priority
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.codehaus.jackson.map.ObjectMapper
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
def priorityManager = ComponentAccessor.getComponent(PriorityManager)
def baseUrls = ComponentAccessor.getComponent(JiraBaseUrls)
ComponentManager.instance
priority(
httpMethod: "POST", groups: ["jira-administrators"]
) {MultivaluedMap queryParams, String body ->
def mapper = new ObjectMapper()
def bean = mapper.readValue(body, PriorityJsonBean)
assert bean.name // must provide priority name
assert bean.description // must provide priority description
assert bean.iconUrl // must provide priority icon url
assert bean.statusColor // must provide priority statusColor
Priority priority
try {
priority = priorityManager.createPriority(bean.name, bean.description, bean.iconUrl, bean.statusColor)
} catch (e) {
return Response.serverError().entity([error: e.message]).build()
}
return Response.created(new URI("/rest/api/2/priority/${priority.id}")).build()
}
But it only creates the specific Priority JSON object, so I would like to know is there any class API I could use to create a general JSON object as the client will pass a general JSON to this REST endpoint like:
{"product_name":"xxxxx", product_id:1234 }
Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My problem is solved using groovy.json.JsonSlurper class.
Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
Could you tell me how you did it? I created rest endpoint but I do not know how to catch incoming json
Thank you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.