如何向Jackson默认反序列化添加行为?

klr1opcd  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(206)

我有三门课:

  1. @JsonIgnoreProperties(ignoreUnknown = true)
  2. @JsonTypeInfo(
  3. use = JsonTypeInfo.Id.NAME,
  4. include = JsonTypeInfo.As.EXISTING_PROPERTY,
  5. property = "type"
  6. )
  7. @JsonSubTypes(value = {
  8. @JsonSubTypes.Type(value = CaseBO.class, name = "case"),
  9. @JsonSubTypes.Type(value = CallBO.class, name = "call"),
  10. })
  11. abstract class BusinessObject {
  12. private Map<String, Object> properties = new HashMap<>();
  13. private String type;
  14. public String getType() {
  15. return this.type;
  16. }
  17. public void setType(String type) {
  18. this.type = type;
  19. }
  20. public Map<String, Object> getProperties() {
  21. return properties;
  22. }
  23. public void setProperties(Map<String, Object> properties) {
  24. this.properties = properties;
  25. }
  26. }
  27. class CallBO extends BusinessObject {
  28. private String content;
  29. public String getContent() {
  30. return this.content;
  31. }
  32. public void setContent(String content) {
  33. this.content = content;
  34. }
  35. }
  36. class CaseBo extends BusinessObject {
  37. private String caseId;
  38. public String getCaseId() {
  39. return caseId;
  40. }
  41. public void setCaseId(String caseId) {
  42. this.caseId = caseId;
  43. }
  44. }

我想要的是:
除了默认的继承反序列化(将json字段和它们的值放入POJO的bean属性中)之外,我还想将每个字段都放入属性Map中。那么我如何用最少的代码来完成这一点呢?

1sbrub3j

1sbrub3j1#

我想知道为什么你要把派生类中的字段放到属性中?CallBO和CaseBO是为了把它们唯一的参数分开,这样就不会互相干扰了,所以为什么要把它们放在一起呢?

相关问题