您好我希望将我的JSON对象存储在商店中,我希望readValue
使用Jackson mapper
但是在运行程序时它会抛出
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "resourceType" (Class io.github.semr.proflr.api.entity.Profile), not marked as ignorable
at [Source: java.io.FileInputStream@46ef0a4f; line: 2, column: 18] (through reference chain: io.github.semr.proflr.api.entity.Profile["resourceType"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1909)
at io.github.semr.proflr.impl.ProfileImpl.add(ProfileImpl.java:41)
at io.github.semr.proflr.impl.ProfileImpl.start(ProfileImpl.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.dm.InvocationUtil.invokeMethod(InvocationUtil.java:111)
at org.apache.felix.dm.InvocationUtil.invokeCallbackMethod(InvocationUtil.java:66)
at org.apache.felix.dm.impl.ComponentImpl.invokeCallbackMethod(ComponentImpl.java:688)
at org.apache.felix.dm.impl.ComponentImpl.invoke(ComponentImpl.java:679)
at org.apache.felix.dm.impl.ComponentImpl.bindService(ComponentImpl.java:624)
at org.apache.felix.dm.impl.ComponentImpl.access$400(ComponentImpl.java:52)
at org.apache.felix.dm.impl.ComponentImpl$7.run(ComponentImpl.java:181)
at org.apache.felix.dm.impl.SerialExecutor$1.run(SerialExecutor.java:47)
at org.apache.felix.dm.impl.SerialExecutor.scheduleNext(SerialExecutor.java:84)
at org.apache.felix.dm.impl.SerialExecutor.access$000(SerialExecutor.java:33)
at org.apache.felix.dm.impl.SerialExecutor$1.run(SerialExecutor.java:50)
at org.apache.felix.dm.impl.SerialExecutor.scheduleNext(SerialExecutor.java:84)
at org.apache.felix.dm.impl.SerialExecutor.execute(SerialExecutor.java:68)
at org.apache.felix.dm.impl.ComponentImpl.calculateStateChanges(ComponentImpl.java:231)
at org.apache.felix.dm.impl.ComponentImpl.start(ComponentImpl.java:399)
at org.apache.felix.dm.DependencyManager.add(DependencyManager.java:169)
at io.github.semr.proflr.impl.Activator.init(Activator.java:14)
at org.apache.felix.dm.DependencyActivatorBase.start(DependencyActivatorBase.java:76)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
at aQute.launcher.Launcher.update(Launcher.java:378)
at aQute.launcher.Launcher.activate(Launcher.java:303)
at aQute.launcher.Launcher.run(Launcher.java:193)
at aQute.launcher.Launcher.main(Launcher.java:89)
我搜索了它并得到了@JsonIgnoreProperties(ignoreUnknown = true)
作为解决方案,但当我将其插入我的代码时,它会给enumExpectedError
。
我认为我没有正确使用注释我如何在我的代码中使用它。
private ObjectMapper mapper = new ObjectMapper();
@Override
public Profile add() {
// read from file, convert it to user class
Profile bean = null;
@JsonIgnoreProperties(ignoreUnknown = true) // I am using it here but show enum expected error
try {
File json = new File("C:\\profile.json");
FileInputStream is = new FileInputStream(json);
bean = mapper.readValue(is,Profile.class);
return bean;
} catch (IOException e) {
e.printStackTrace();
}
return bean;
}
答案 0 :(得分:3)
必须在要反序列化的类上使用@JsonIgnoreProperties
注释,并且您愿意忽略未知字段:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Profile {
// ...
}