Search code examples
javagroovyjiraunirest

Updating Jira Cloud using the Unirest API


I am able to access Jira cloud using Unirest Java library and basic authentication. Thanks to the Stackflowers for making it possible.

I would like to update one sample ticket's priority using the same. Below is the groovy code. Its not working but. Could you please identify where is my mistake.

     result = 
     Unirest.put("https://mysite.atlassian.net/rest/api/2/issue/TIC-1")
    .basicAuth("myuser","mypwd")
    .header('Content-Type', 'application/json')
    .body([fields: [priority: [name: 'High']]]).asString

     println result.status

Error displayed is:

enter code hereCaught: java.lang.RuntimeException: Serialization Impossible. 
Can't find an ObjectMapper implementation.
java.lang.RuntimeException: Serialization Impossible. Can't find an 
ObjectMapper implementation.
com.mashape.unirest.request.HttpRequestWithBody.body(HttpRequestWithBody.jav
a:155)
com.mashape.unirest.request.HttpRequestWithBody$body$1.call(Unknown Source)
JiraClient.run(JiraClient.groovy:51)

All Maven dependencies added already and Unirest.get() request/responses working properly.


Solution

  • Above code works. We need to implement Unirest ObjectMapper method. I am using the Java method as is from the below reference. It works with Groovy script as is:

    /*Ref: <http://blog.pikodat.com/2015/09/11/tooltip-unirest-lightweight-http-
    request-client-libraries/>*/
    
    Unirest.setObjectMapper(new ObjectMapper() {
    private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = 
    new com.fasterxml.jackson.databind.ObjectMapper();
    
    public <T> T readValue(String value, Class<T> valueType) {
        try {
            return jacksonObjectMapper.readValue(value, valueType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    public String writeValue(Object value) {
        try {
            return jacksonObjectMapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    });
    

    And you need to have additional Maven dependencies on the Jackson-bind:

    <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>2.8.8</version>
    </dependency>
    

    As a last step, add the missing import statements:

    import com.fasterxml.jackson.core.JsonProcessingException
    import com.mashape.unirest.http.ObjectMapper
    import com.mashape.unirest.http.Unirest
    

    And bingo! You can watch Jira updated externally in no time! Enjoy!!