如何用jackson传递构造函数的参数? [英] How to pass constructor's parameters with jackson?
问题描述
我正在尝试使用杰克逊去除对象
i am trying to desearlize an object using Jackson
this.prepareCustomMapper().readValue(response.getBody(), EmailResponse.class);
我有这个例外:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.despegar.social.automation.services.emailservice.response.EmailResponse]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@4f38f663; line: 1, column: 12] (through reference chain: com.despegar.social.automation.services.emailservice.response.EmailsResponse["items"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
我知道发生了这种情况,因为这是我的构造函数:
I know that this is happening because this is my constructor:
public class EmailResponse extends MyServiceResponse {
private String id;
private String user_id;
private String email;
private Boolean is_primary;
private Boolean is_confirmed;
public EmailResponse(HttpResponse request) {
super(request);
}
}
所以,我的构造函数收到HttpResponse参数而我不是通过它,但我不知道该怎么做。我不能用空构造函数过度充电,因为我需要以这种方式接收HttpResponse对象。
当我调用readValue()方法时,有没有办法传递这个构造函数参数?或者在这种情况下最好的选择是什么?我感谢您的帮助。问候
So, my constructor recieve HttpResponse parameter and i am not passing it, but i don't know how to do it. I cant overcharge with an empty constructor because i need that to recieve HttpResponse object at this way. Is there any way to pass this constructor param when i call readValue() method? Or what could be the best option at this case? I appreciate your help. Regards
推荐答案
你可以使用 Jackson值注入功能传递一个对象引用,该对象引用不是输入JSON的一部分作为构造函数参数。下面是一个示例:
You can use the Jackson value injection feature to pass an object reference which is not a part of the input JSON as a constructor parameter. Here is an example:
public class JacksonInjectExample {
private static final String JSON = "{\"field1\":\"value1\", \"field2\":123}";
// HttpResponse in your case
public static class ExternalObject {
@Override
public String toString() {
return "MyExternalObject";
}
}
public static class Bean {
// make fields public to avoid writing getters in this example
public String field1;
public int field2;
private ExternalObject external;
public Bean(@JacksonInject final ExternalObject external) {
this.external = external;
}
@Override
public String toString() {
return "Bean{" +
"field1='" + field1 + '\'' +
", field2=" + field2 +
", external=" + external +
'}';
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final InjectableValues.Std injectableValues = new InjectableValues.Std();
injectableValues.addValue(ExternalObject.class, new ExternalObject());
mapper.setInjectableValues(injectableValues);
final Bean bean = mapper.readValue(JSON, Bean.class);
System.out.println(bean);
}
}
输出:
Bean{field1='value1', field2=123, external=MyExternalObject}
这篇关于如何用jackson传递构造函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!