user2428795
user2428795

Reputation: 547

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]

Upvotes: 6

Views: 17021

Answers (4)

The process of deserialization (Conversion of stream to java object) will access default constructor that is called for the first class in the inheritance hierarchy that does not implement interface Serializable.

Hence all that you need to resolve is a default constructor (no args/parameterless). THis article will help you understand better : https://docs.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html#4539

Upvotes: 1

matsev
matsev

Reputation: 33797

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
 }

Upvotes: 13

user2428795
user2428795

Reputation: 547

I needed to add a dummy constructor

public ErrorHolder(){
}

Upvotes: 0

Hakan Serce
Hakan Serce

Reputation: 11266

I believe that you need to add a no parameter constructor to your ErrorHolder class like this:

public ErrorHolder(){
   this(null);
}

Upvotes: 3

Related Questions