Why am I getting org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type
Solution 1
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
}
Solution 2
I believe that you need to add a no parameter constructor to your ErrorHolder
class like this:
public ErrorHolder(){
this(null);
}
Solution 3
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
user2428795
Updated on June 09, 2022Comments
-
user2428795 almost 2 years
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]