Java和@jsonproperty
我想知道是否有一种方法可以将 @JsonProperty 应用于类的继承字段。 例如我有以下课程。
public class Person {
protected String id;
protected String firstName;
protected String middleName;
protected String lastName;
}
...
/**
* need to figure out here, how to apply @JsonProperty
* to the fields below of the parent class:
* protected String id;
* protected String firstName;
* protected String middleName;
* protected String lastName;
*/
public class Employee extends Person {
@JsonProperty("DepartmentID")
protected String departmentId;
}
这里的目标是只有 Employee 类才会有 @JsonProperty 而不是 Person 类。
谢谢。
I am wondering if there is a way to apply @JsonProperty to an inherited fields of a class.
For example I have the following classes.
public class Person {
protected String id;
protected String firstName;
protected String middleName;
protected String lastName;
}
...
/**
* need to figure out here, how to apply @JsonProperty
* to the fields below of the parent class:
* protected String id;
* protected String firstName;
* protected String middleName;
* protected String lastName;
*/
public class Employee extends Person {
@JsonProperty("DepartmentID")
protected String departmentId;
}
the goal here is that only Employee class will have @JsonProperty not the Person class.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用Getters和Setter,并注释您的Getter而不是字段。杰克逊将根据getter名称自动检测设置器。
Use getters and setters and annotate your getter instead of the field. Jackson will automatically detect the setter based on the getter name.
@jsonproperty的覆盖方法
Override getter methods with @JsonProperty