Jackson:忽略父类属性:将json反序列化为数据(Jackson: Parent class Properties are ignored: Deserializes json into data)
我试图将JSON(myJson.json)对象转换为Java类。
使用以下如何将Java对象转换为来自Json Jackson @Mykong的代码。
//2. Convert JSON to Java object ObjectMapper mapper = new ObjectMapper(); StudentInfoDTO student = mapper.readValue(new File("c:\\myJson.json"), StudentInfoDTO.class);
但是上面的
ObjectMapper
unable to map the Parent(StudentDTO) Class Properties(loggedInUser,createdBy,...).
是否有任何
@annotations
也可以映射父类属性。欢迎任何建议。
我有以下 - StudentInfoDTO,AddressDTO,StudentDTO,SkillsDTO和myJson.json。
StudentInfoDTO:
public class StudentInfoDTO extends StudentDTO implements Serializable{ private static final long serialVersionUID = 1L; private AddressDTO address; private String studentName; ....getters and setters }
AddressDTO:
public class AddressDTO implements Serializable{ private static final long serialVersionUID = 1L; private String street; private String city; private String state; private String country; private Long zipcode; ....getters and setters }
StudentDTO:
public class StudentDTO implements Serializable{ private static final long serialVersionUID = 1L; private String loggedInUser; private String createdBy; private Date createdTs; private String updatedBy; private Date updatedTs; private List<skillsDTO> skills; ....getters and setters }
myJson.json
{ "address": { "street": "Hunterfly Place", "city": "Dunlo", "state": "Arizona", "country": "Colorado", "zipcode": "1631", }, "studentName": "John", "loggedInUser": "Admin", "createdBy": "System", "createdTs": "03-19-2011 14:53", "updatedBy": "John", "updatedTs": "06-11-2014 08:00", "skills": [ { "skill": "Football", "grade": "A" }, { "skill": "Swimming", "grade": "C" }, { "skill": "Dance", "grade": "A" }] }
我的错误日志:
03:29:03,132 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "loggedInUser" (Class com.mySchool.project.services.StudentInfoDTO), not marked as ignorable 03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at [Source: c:\myJson.json; line: 51, column: 23] (through reference chain: com.mySchool.project.services.StudentInfoDTO["address"]->com.mySchool.project.services.StudentInfoDTO["loggedInUser"]) 03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) 03:29:03,134 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:246)
I am trying to convert the JSON(myJson.json) object into Java Class.
Using the following How to convert Java Object to From Json Jackson @Mykong code.
//2. Convert JSON to Java object ObjectMapper mapper = new ObjectMapper(); StudentInfoDTO student = mapper.readValue(new File("c:\\myJson.json"), StudentInfoDTO.class);
But the above
ObjectMapper
isunable to map the Parent(StudentDTO) Class Properties(loggedInUser,createdBy,...).
Are there any
@annotations
which can map the Parent Class Properties as well.Any suggestions are welcome.
I am having the following - StudentInfoDTO, AddressDTO, StudentDTO, SkillsDTO and myJson.json.
StudentInfoDTO:
public class StudentInfoDTO extends StudentDTO implements Serializable{ private static final long serialVersionUID = 1L; private AddressDTO address; private String studentName; ....getters and setters }
AddressDTO:
public class AddressDTO implements Serializable{ private static final long serialVersionUID = 1L; private String street; private String city; private String state; private String country; private Long zipcode; ....getters and setters }
StudentDTO:
public class StudentDTO implements Serializable{ private static final long serialVersionUID = 1L; private String loggedInUser; private String createdBy; private Date createdTs; private String updatedBy; private Date updatedTs; private List<skillsDTO> skills; ....getters and setters }
myJson.json
{ "address": { "street": "Hunterfly Place", "city": "Dunlo", "state": "Arizona", "country": "Colorado", "zipcode": "1631", }, "studentName": "John", "loggedInUser": "Admin", "createdBy": "System", "createdTs": "03-19-2011 14:53", "updatedBy": "John", "updatedTs": "06-11-2014 08:00", "skills": [ { "skill": "Football", "grade": "A" }, { "skill": "Swimming", "grade": "C" }, { "skill": "Dance", "grade": "A" }] }
My Error Log:
03:29:03,132 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "loggedInUser" (Class com.mySchool.project.services.StudentInfoDTO), not marked as ignorable 03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at [Source: c:\myJson.json; line: 51, column: 23] (through reference chain: com.mySchool.project.services.StudentInfoDTO["address"]->com.mySchool.project.services.StudentInfoDTO["loggedInUser"]) 03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) 03:29:03,134 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:246)
原文:https://stackoverflow.com/questions/26992338
最满意答案
decls
不包含继承的成员; 你正在寻找members
。
decls
doesn't include inherited members; you are looking formembers
.
相关问答
更多-
像这样: typeof(Typ).BaseType.Name Like so: typeof(Typ).BaseType.Name
-
理论上讲,通过检索所有加载的类并检查其中哪些是从Emp派生并包含该字段是非常复杂和昂贵的方法。 如果所需的班级尚未加载,这可能也无济于事。 In theory there is a very complicated and costly way by retrieving all loaded classes and checking which of them are derived from Emp and contain the field. If the desired class wasn't l ...
-
通过反射获取特定的公共字段(适用于所有父类)(Get particular public fields via reflection (for all parent classes))[2021-12-12]
decls不包含继承的成员; 你正在寻找members 。 decls doesn't include inherited members; you are looking for members. -
使用Java反射从内部类修改嵌套类的字段(Using Java reflection to modify nesting class's fields from inner class)[2024-03-07]
我无法弄清楚什么是第二人称场 好吧,从那以后 public class InnerClass extends OuterClass { // ^^^^^^^^^^^^^^^^^^ 你的InnerClass还继承了自己设置的person字段 public void aPerson(Integer age) { person = new Person(age); } 因为person引用this.person , this方法中的this表示实现方法的类的实 ... -
如何访问匿名对象中声明的字段而不反射?(How to access fields declared inside anonymous object without reflection?)[2023-06-29]
喜欢这个 class A { public static void main(String[] args) { Object o = new Object() { public int x = 0; { System.out.println("x: " + x++); System.out.println("x: " + x++); } }; ... -
Assembly assem = typeof(Parent).Assembly; foreach (var type in assem.GetTypes().Where(x => x.IsSubclassOf(typeof(Parent)))) { Console.WriteLine($"Parent \"{type.Name}\" found!"); } Console.ReadLine(); Assembly assem = typeof(Parent).Assembly; foreach ...
-
使用StackTrace您至少可以获得进行调用的方法和类: System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(); Type calledFromType = trace.GetFrame(1).GetMethod().ReflectedType; 这应该给你Parent1类型。 我认为没有办法获取调用该方法的变量的名称。 您当然可以枚举calledFromType所有字段和属性,并查看其中一个是否属于Child ...
-
如何通过Android上的反射实例化成员类(How can I instantiate a member class through reflection on Android)[2023-07-03]
好吧,事实证明我正在咆哮着错误的树。 我概述的方法在Android上以及其他任何地方都能很好地运行。 问题是我在这种情况下试图实例化的类是静态内部类。 因此,虽然它是一个成员类,但它没有引用它的外部类。 我只需要在查找合成构造函数/字段之前检查if (!Modifier.isStatic(objectClass.getModifiers())) 。 Okay, turns out I was barking up the wrong tree. The method I've outlined works ... -
您可以使用getType方法确定字段类型,然后仅使用必填字段。 对于此特定情况,您可以检查归档是否类型为Date或String 。 编辑: 使用注释+反射 第1步:定义自定义注释。 这里DesiredField是我们的自定义注释 @Target(value = ElementType.FIELD) @Retention(value = RetentionPolicy.RUNTIME) public @interface DesiredField { } 第2步:使用DesiredField注释适当 ...
-
Java Reflection API。(Java Reflection API. Cant instantiate Inner Class if its not public)[2022-05-22]
您忘记将构造函数设置为可访问 这应该工作: Constructor> ctor = innerReal.getConstructor(outerClazz.getClass(), String.class); ctor.setAccessible(true); // can throw some sort of Exception AbstractClass resultClass = (AbstractClass) ctor.newInstance(outerClazz, innerReal.getN ...