0

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?

1 Answer 1

1
// 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.
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, but it is giving another exception. See this : Exception in thread "main" java.lang.RuntimeException: Serialization Impossible. Can't find an ObjectMapper implementation. at com.mashape.unirest.request.HttpRequestWithBody.body(HttpRequestWithBody.java:155) at CreateIssue.main(CreateIssue.java:42)
Hey ,In Your code you have imported object objectMapper remove it and then try
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; import org.json.JSONObject;
Use The Above Libraries
Thank you @NISHANT DESAI, I was importing wrong libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.