如何使用json通过rest api更新Jira中的描述

时间:2016-06-22 12:29:02

标签: java json jira jira-rest-api jira-rest-java-api

以下是包含富文本/维基文本的JSON数据。我想通过REST API将此数据传递给Jira中的一个问题。这里的Java是我正在使用的技术。

{"update":{"summary": [{"set": "CRF-397 – For Virgin Mobile, alert must be sent via email when Tier Mismatch exception is encountered."}]},"fields":{"description":{"set":"*Clients Impacted** Virgin Mobile *Background Information*<br>All UK schemes are experiencing at different levels some issues of:* Customers being billed on the wrong premium* Excess Fees paid at point of claim do not correspond to what has been communicated to the customer at the point of sale.* Welcome packs not being issued due to a mismatch *CRF Scope*<br>The scope of this project consists of identifying whenever a new device is communicated to Asurion by a client system and ensuring the data in each of those instances is validated to confirm that the device premium and excess fees are correctly aligned.*User Story Scope*<br>While doing enrollment if any record goes into exception due to Tier is match we have to send consolidated list of such records via email so that Asurion Team can communicate with Virgin Mobile to resolve the Tier Mismatch issues.*Requirement** An email alert must be sent when Tier Mismatch exception is encountered.* Flag based development must be done for triggering an email.* Email must be sent to Client Service, SCM and BI teams* Recipient email IDs must be configurable.* Exception list must contain below records:-      * The list of devices for which there was an exception * The Feature Code sent by Virgin Mobile * The feature code configured in Client Convention for the given device*"}}}

以上JSON我存储在jiraUpdateFromBuilder

我正在调用PUT方法来更新Jira中的说明,如下所示。

String _jiraUrl = applicationProperties.get(Constants.JIRAURL)
            + "/rest/api/2/issue/" + reference;
String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
int statusCode = invokePutMethod(auth, _jiraUrl.trim(),
            jiraUpdateFromBuilder.toString().trim());

public static int invokePutMethod(String auth, String url, String data) {

    int statusCode = 0;
    try {
        Client client = Client.create();
        WebResource webResource = client.resource(url);
        ClientResponse response = webResource
                .header("Authorization", "Basic " + auth)
                .type("application/json").accept("application/json")
                .put(ClientResponse.class, data);
        statusCode = response.getStatus();
        return statusCode;
    } catch (Exception e) {
        Constants.ERROR.info(Level.INFO, e);

    }
    return statusCode;
}

这样做,我无法通过任何REST API更新Jira中的问题描述,因为此处获取201以外的状态。同样的问题是JIRA中包含富文本的问题的所有字段。如果我需要更改JSON或任何其他方法,请告诉我JRJC是否可以提供帮助。

1 个答案:

答案 0 :(得分:1)

你的json看起来像这样:

{
  "update": {
    "summary": [
      {
        "set": "CRF-397 ..."
      }
    ]
  },
  "fields": {
    "description": {
      "set": "..."
    }
  }
}

但&#34;字段&#34;部分不需要使用&#39; set&#39;关键字,所以它应该是这样的:

{
  "update": {
    "summary": [
      {
        "set": "CRF-397 ..."
      }
    ]
  },
  "fields": {
    "description": "..."
  }
}

如果您查看PUT /issue REST resource的文档,则会发现它提到了这一点:

  

在&#34;字段中指定&#34; field_id&#34;:field_value&#34;是&#34; set&#34;的缩写。操作&#34;更新&#34;部分。   字段应出现在&#34;字段&#34;或者&#34;更新&#34;,而不是两者。

此外,您已经提到您的回复状态代码为400,这意味着它是一个错误的请求。响应机构可能会包含有关错误的更多详细信息,因此最好将其记录下来。

关于此错误:

  

非法的非引用字符((CTRL-CHAR,代码10)):必须使用反斜杠进行转义,以包含在[Source:org.apache.catalina.connector.CoyoteInputStream@20e841d2;中的字符串值\ n中; line:1,column:187]

您的描述值包含换行符,但不允许直接在json字符串中使用它们。你必须逃避这些。请参阅this post for an example