使用Unirest API更新Jira Cloud

时间:2017-04-28 06:00:48

标签: java groovy jira unirest

我可以使用Unirest Java库和基本身份验证访问Jira云。感谢Stackflowers使其成为可能。

我想使用相同的方式更新一张样本票的优先级。下面是groovy代码。它不起作用但是。能不能确定我的错误在哪里。

     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

显示的错误是:

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)

已添加所有Maven依赖项,Unirest.get()请求/响应正常工作。

1 个答案:

答案 0 :(得分:0)

以上代码有效。我们需要实现Unirest ObjectMapper方法。我正在使用Java方法,如下面的参考。它适用于Groovy脚本:

/*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);
    }
}
});

你需要在Jackson-bind上有额外的Maven依赖:

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

最后一步,添加缺少的导入语句:

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

和宾果!您可以立即在外部观看Jira的更新!享受!!