簡體   English   中英

Json Json Java中使用atlassian的REST API創建Jira問題時解析Json Object節點時出錯

[英]Error in parsing Json Object node while creating an issue of Jira using REST API of atlassian in Java

我正在嘗試在 java 中使用 Jira REST API 創建問題。但我在控制台中遇到運行時異常。

Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Unexpected IOException (of type org.codehaus.jackson.JsonParseException): Illegal white space character (code 0x20) as character #3 of 4-char base64 unit: can only used between units
 at [Source: N/A; line: -1, column: -1]
    at CreateIssue$1.writeValue(CreateIssue.java:60)
    at com.mashape.unirest.request.HttpRequestWithBody.body(HttpRequestWithBody.java:158)
    at CreateIssue.main(CreateIssue.java:72)

下面是我的代碼,我已經實現了與文檔中提供的代碼相同的代碼,但仍然出現錯誤。

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.http.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

import java.io.IOException;

public class CreateIssue{
    public static void main(String[]args) {
        JsonNodeFactory jnf = JsonNodeFactory.instance;
        ObjectNode payload = jnf.objectNode();

        {
            ObjectNode fields = payload.putObject("fields");
            {
                fields.put("summary", "This is test Summary");
                ObjectNode project = fields.putObject("project");
                {
                    project.put("key", "DEM12");
                }
                ObjectNode issueType = fields.putObject("issuetype");
                {
                    issueType.put("name", "Task");
                }
            }

        }
        // Connect Jackson ObjectMapper to Unirest
        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);
                }
            }
        });

        System.out.println("Payload from here: "+ payload);

        HttpResponse<com.mashape.unirest.http.JsonNode> response = null;
        try {
            response = Unirest.post("https://{myaccount}.atlassian.net//rest/api/3/issue")
                        .basicAuth("{myEmailId}", "{MyAPITOken}")
                    .header("Accept", "application/json")
                    .body(payload)
                    .asJson();


            System.out.println(response.getBody());
            System.out.println(response.getStatus());
        } catch (UnirestException e) {
            e.printStackTrace();
        }
    }
}

我在這里打印的 output 是真正的 json,如果我使用 output 在Postman中發送帖子請求,它工作正常 所以我認為錯誤在我的代碼中某處,但我不知道在哪里。 有人能幫忙嗎?

// Connect Jackson ObjectMapper to Unirest
//Remove This Block Of Code first.
        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);
                }
            }
        });

//Then Add This Code.
//Use ObjectNode

ObjectNode obj = payload;
JSONObject json = new JSONObject(obj.toString());

HttpResponse<com.mashape.unirest.http.JsonNode> response = null;
        try {
            response = Unirest.post("https://{myaccount}.atlassian.net//rest/api/3/issue")
                        .basicAuth("{myEmailId}", "{MyAPITOken}")
                    .header("Accept", "application/json")
                    .body(json)
                    .asJson();


            System.out.println(response.getBody());
            System.out.println(response.getStatus());
        } catch (UnirestException e) {
            e.printStackTrace();
        }

I think so it will work.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM