jackson AfterburnerModule在日志中给出警告
问题描述:
启用AfterburnerModule后,我在日志中看到下面的警告消息。没有AfterburnerModule一切正常。jackson AfterburnerModule在日志中给出警告
Sep 06, 2017 9:11:39 AM com.fasterxml.jackson.module.afterburner.deser.BeanPropertyMutator _reportProblem
WARNING: Disabling Afterburner deserialization for class com.test.Child (field #0; mutator com.test.Child$Access4JacksonDeserializerdc6e0fab), due to access error (type java.lang.IllegalAccessError, message=tried to access method com.test.test2.Parent.setSurname(Ljava/lang/String;)V from class com.test.Child$Access4JacksonDeserializerdc6e0fab)
java.lang.IllegalAccessError: tried to access method com.test.test2.Parent.setSurname(Ljava/lang/String;)V from class com.test.Child$Access4JacksonDeserializerdc6e0fab
at com.test.Child$Access4JacksonDeserializerdc6e0fab.stringSetter(com/test/Child$Access4JacksonDeserializer.java)
at com.fasterxml.jackson.module.afterburner.deser.BeanPropertyMutator.stringSetter(BeanPropertyMutator.java:123)
at com.fasterxml.jackson.module.afterburner.deser.SettableStringMethodProperty.deserializeAndSet(SettableStringMethodProperty.java:60)
at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:156)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3807)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2797)
at com.test.JacksonStuff.main(JacksonStuff.java:19)
我班如下:
package com.test.test2;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Parent {
@JsonProperty("surname")
private String surname;
protected Parent() {
}
public Parent(String surname) {
this.surname = surname;
}
public String getSurname() {
return surname;
}
protected void setSurname(String surname) {
this.surname = surname;
}}
package com.test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.test.test2.Parent;
public class Child extends Parent {
@JsonProperty("name")
private String test;
public Child() {
super();
}
public Child(String var1) {
super(var1);
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}}
package com.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import java.io.IOException;
public class JacksonStuff {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
AfterburnerModule module = new AfterburnerModule();
mapper.registerModule(module);
String json = "{\"surname\":\"abc\"}";
Child value = mapper.readValue(json, Child.class);
}}
如何避免让在日志中这样的警告?我不能修改parent.java,因为它来自第三方库,不受我控制。
答
因为void setSurname
是protected
。将其设置为public
。
正如我所说我不能编辑Parent.java。到目前为止我发现的一些解决方案:Child.java和Parent.java应该位于同一个包中。将父#setSurname()的可见性从protected更改为public。并将@JsonIgnore注释添加到父级#setSurname()。 – anuj