在对象内部创建相同类型的实体对象

huangapple 未分类评论76阅读模式
英文:

Create Entity for Objects of same Type inside an Object

问题

我想使用Lombok表示一个复杂的JSON对象:

{
  "property1": {
    "type": "boolean",
    "value": true,
    "valueInfo": {
      "info": "Hello"
    }
  },
  "property2": {
    "type": "string",
    "value": "string234",
    "valueInfo": {
      "info": "World"
    }
  }
}

我已经成功创建了一个实体,可以使用外部键来表示复杂对象:

"properties": {
  "property1": {
    "type": "boolean",
    "value": true,
    "valueInfo": {
      "info": "Hello"
    }
  },
  "property2": {
    "type": "string",
    "value": "string234",
    "valueInfo": {
      "info": "World"
    }
  }
}

Lombok Java代码:

public class ComplexObject {

@JsonProperty("properties")
@Valid
private Map<String, PropertyValueDTO> variables = null;
}

public static class PropertyValueDTO {
  @JsonProperty("type")
  private ProcessVariableTypeEnum type;

  @JsonProperty("value")
  private String value;

  @JsonProperty("valueInfo")
  @Valid
  private Map<String, Object> valueInfo = null;
}

是否可以在不需要键("properties")的情况下,从基本实体类中表示PropertyValueDTO

英文:

I want to represent a complex JSON Object using Lombok:

{
  &quot;property1&quot;: {
    &quot;type&quot;: &quot;boolean&quot;,
    &quot;value&quot;: true,
    &quot;valueInfo&quot;: {
      &quot;info&quot;: &quot;Hello&quot;
    }
  },
  &quot;property2&quot;: {
    &quot;type&quot;: &quot;string&quot;,
    &quot;value&quot;: &quot;string234&quot;,
    &quot;valueInfo&quot;: {
      &quot;info&quot;: &quot;World&quot;
    }
  }
}

I was able to create an Entity when I can represent the complex object using an outer key:

&quot;properties&quot;: {
  &quot;property1&quot;: {
    &quot;type&quot;: &quot;boolean&quot;,
    &quot;value&quot;: true,
    &quot;valueInfo&quot;: {
      &quot;info&quot;: &quot;Hello&quot;
    }
  },
  &quot;property2&quot;: {
    &quot;type&quot;: &quot;string&quot;,
    &quot;value&quot;: &quot;string234&quot;,
    &quot;valueInfo&quot;: {
      &quot;info&quot;: &quot;World&quot;
    }
  }
}

Lombok Java Code:

public class ComplexObject {

@JsonProperty(&quot;properties&quot;)
@Valid
private Map&lt;String, PropertyValueDTO&gt; variables = null;
}

public static class PropertyValueDTO {
  @JsonProperty(&quot;type&quot;)
  private ProcessVariableTypeEnum type;

  @JsonProperty(&quot;value&quot;)
  private String value;

  @JsonProperty(&quot;valueInfo&quot;)
  @Valid
  private Map&lt;String, Object&gt; valueInfo = null;

}

Is it possible to represent PropertyValueDTO from a base entity class without the need for key (properties)?

huangapple
  • 本文由 发表于 2020年7月27日 22:06:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63117077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定