在 Java 中使用 atlassian 的 REST API 创建 Jira 问题时解析 Json 对象节点时出错
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.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();
}
}
}
我在这里打印的输出是真实的 json,如果我使用该输出将 post 请求放入 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.
我正在尝试在 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.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();
}
}
}
我在这里打印的输出是真实的 json,如果我使用该输出将 post 请求放入 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.