I am trying to create an issue using Jira REST API in java. But I am getting below Runtime Exception in my console.
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)
Below is my code, I have implemented same code as provided in the documentation, still I am getting an error.
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();
}
}
}
The output which I am printing here, is genuine json and if I put post request in Postman using that output, it is working fine. So I think error is lying in my code somewhere, but I don't know where. Can anyone help with this?
CodePudding user response:
// 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.