Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type

Can someone please tell me Why am I getting org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type error?

Here is my call:

try
        {
            String jsonreturn = restTemplate.getForObject("http://" + mRESTServer.getHost() + ":8080/springmvc-rest-secured-test/json/{name}", String.class, vars);
            LOGGER.debug("return object:  " + jsonreturn.toString());
        } catch (HttpClientErrorException e)
        {
            /**
             *
             * If we get a HTTP Exception display the error message
             */

            LOGGER.error("error:  " + e.getResponseBodyAsString());

            ObjectMapper mapper = new ObjectMapper();
            ErrorHolder eh = mapper.readValue(e.getResponseBodyAsString(), ErrorHolder.class);

            LOGGER.error("error:  " + eh.errorMessage);

        }

which I am trying to test the error so I should be creating a ErrorHolder Object but I am getting the error;

Here is my ErrorHolder class:

public class ErrorHolder
{


    public String errorMessage;

    public ErrorHolder(String errorMessage)
    {
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage()
    {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage)
    {
        this.errorMessage = errorMessage;
    }

    @Override
    public String toString()
    {
        return "ErrorHolder{" +
                "errorMessage='" + errorMessage + '\'' +
                '}';
    }
}

I dont know why I am getting the following error:

2013-06-12 14:36:32,138 [main] ERROR Main - error:  {"errorMessage":"Uh oh"}
Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class ErrorHolder]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: java.io.StringReader@628016f7; line: 1, column: 2]
like image 889
user2428795 Avatar asked Jun 12 '13 18:06

user2428795


People also ask

Does Jackson need default constructor?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.

What is JsonMappingException?

public class JsonMappingException extends JsonProcessingException. Checked exception used to signal fatal problems with mapping of content, distinct from low-level I/O problems (signaled using simple IOException s) or data encoding/decoding problems (signaled with JsonParseException , JsonGenerationException ).

What is default constructor Java?

Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined.


1 Answers

Two options, either you provide a default no-argument constructor which does the job. However, for your use-case a nicer solution IMHO is provided by @JsonCreator and @JsonProperty:

public class ErrorHolder
{
    public String errorMessage;

    @JsonCreator
    public ErrorHolder(@JsonProperty("errorMessage") String errorMessage)
    {
        this.errorMessage = errorMessage;
    }

    // getters and setters
 }
like image 129
matsev Avatar answered Oct 05 '22 23:10

matsev