java 为什么我会收到 org.codehaus.jackson.map.JsonMappingException:找不到适合类型的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17072927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting org.codehaus.Hymanson.map.JsonMappingException: No suitable constructor found for type
提问by user2428795
Can someone please tell me Why am I getting org.codehaus.Hymanson.map.JsonMappingException: No suitable constructor found for type error?
有人可以告诉我为什么我会收到 org.codehaus.Hymanson.map.JsonMappingException: No合适的构造函数发现类型错误?
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;
我正在尝试测试错误,因此我应该创建一个 ErrorHolder 对象,但出现错误;
Here is my ErrorHolder class:
这是我的 ErrorHolder 类:
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.Hymanson.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]
回答by matsev
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 @JsonCreatorand @JsonProperty:
两个选项,要么提供一个默认的无参数构造函数来完成这项工作。但是,对于您的用例,恕我直言,@JsonCreator和@JsonProperty提供了更好的解决方案:
public class ErrorHolder
{
public String errorMessage;
@JsonCreator
public ErrorHolder(@JsonProperty("errorMessage") String errorMessage)
{
this.errorMessage = errorMessage;
}
// getters and setters
}
回答by Hakan Serce
I believe that you need to add a no parameter constructorto your ErrorHolder
class like this:
我相信您需要像这样为您的类添加一个无参数构造函数ErrorHolder
:
public ErrorHolder(){
this(null);
}
回答by Balaji Boggaram Ramanarayan
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.
反序列化过程(将流转换为 java 对象)将访问默认构造函数,该构造函数为继承层次结构中未实现接口 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
因此,您需要解决的只是一个默认构造函数(无参数/无参数)。这篇文章将帮助您更好地理解:https: //docs.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html#4539
回答by user2428795
I needed to add a dummy constructor
我需要添加一个虚拟构造函数
public ErrorHolder(){
}