Why I get org.codehaus.jackson.map.JsonMappingException: no suitable constructor found for type

Can someone tell me why I get 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 check for an error, so I have to create an ErrorHolder object, but I get an 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 do not 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] 
+6
source share
4 answers

Two options: either you create a default no-argument constructor that does the job. However, for your use case, a nicer IMHO solution 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 } 
+13
source

I believe that you need to add a constructor without parameters to the ErrorHolder class as follows:

 public ErrorHolder(){ this(null); } 
+3
source

I needed to add a dummy constructor

 public ErrorHolder(){ } 
0
source

The deserialization process (converting the stream to a java object) will call the default constructor, which is called for the first class in the inheritance hierarchy, which does not implement the Serializable interface.

Therefore, all you have to solve is the default constructor (no arguments / no parameters). This article will help you better understand: https://docs.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html#4539

0
source

Source: https://habr.com/ru/post/947148/


All Articles