【问题标题】:How to fetch properties from dynamically generated pojo class如何从动态生成的 pojo 类中获取属性
【发布时间】:2016-01-05 14:37:52
【问题描述】:

我正在使用 jsonschema2pojo-maven-plugin v0.4.7 从 JSON 模式生成 POJO 类。我想获取动态生成的 pojo 类的属性。pojo 类包含在另一个 pojo 类中。是否有从包含 onath 的 pojo 类中读取字段的解决方案?例如,在这种情况下,我有 Employee pojo,因为有我想获取两个类的字段的 Address 对象。

下面是我的架构

    {
     "$schema": "http://json-schema.org/draft-04/schema#",
     "type": "object",
     "title":"Employee",
     "name":"Employee",
     "properties": {
     "empId": {
      "type": "integer"
    },
    "lastName": {
      "type": "string"
    },
    "title": {
      "type": "string"
    },
    "salary": {
      "type": "integer"
    },
    "address": {
      "type": "object",
      "properties": {

        "city": {
          "type": "string"
        },
        "pincode": {
          "type": "integer"
        },
        "landMark":{
        "type": "string"
        }
     }

    },
    "phoneNo": {
      "type": "object",
      "properties": {

        "mobile": {
          "type": "integer"
        },
        "landLine": {
          "type": "integer"
        }
     }

    }
  }
}

pojo 类

1.员工

 @JsonInclude(JsonInclude.Include.NON_NULL)
  @Generated("org.jsonschema2pojo")
 @JsonPropertyOrder({
"empId",
"lastName",
"title",
"salary",
"address",
"phoneNo"
})
 public class Employee {

@JsonProperty("empId")
private Integer empId;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("title")
private String title;
@JsonProperty("salary")
private Integer salary;
@JsonProperty("address")
private Address address;
@JsonProperty("phoneNo")
private PhoneNo phoneNo;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 * 
 * @return
 *     The empId
 */
@JsonProperty("empId")
public Integer getEmpId() {
    return empId;
}

/**
 * 
 * @param empId
 *     The empId
 */
@JsonProperty("empId")
public void setEmpId(Integer empId) {
    this.empId = empId;
}

/**
 * 
 * @return
 *     The lastName
 */
@JsonProperty("lastName")
public String getLastName() {
    return lastName;
}

/**
 * 
 * @param lastName
 *     The lastName
 */
@JsonProperty("lastName")
public void setLastName(String lastName) {
    this.lastName = lastName;
}

/**
 * 
 * @return
 *     The title
 */
@JsonProperty("title")
public String getTitle() {
    return title;
}

/**
 * 
 * @param title
 *     The title
 */
@JsonProperty("title")
public void setTitle(String title) {
    this.title = title;
}

/**
 * 
 * @return
 *     The salary
 */
@JsonProperty("salary")
public Integer getSalary() {
    return salary;
}

/**
 * 
 * @param salary
 *     The salary
 */
@JsonProperty("salary")
public void setSalary(Integer salary) {
    this.salary = salary;
}

/**
 * 
 * @return
 *     The address
 */
@JsonProperty("address")
public Address getAddress() {
    return address;
}

/**
 * 
 * @param address
 *     The address
 */
@JsonProperty("address")
public void setAddress(Address address) {
    this.address = address;
}

/**
 * 
 * @return
 *     The phoneNo
 */
@JsonProperty("phoneNo")
public PhoneNo getPhoneNo() {
    return phoneNo;
}

/**
 * 
 * @param phoneNo
 *     The phoneNo
  enter code here     */
@JsonProperty("phoneNo")
public void setPhoneNo(PhoneNo phoneNo) {
    this.phoneNo = phoneNo;
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

@Override
public int hashCode() {
    return new HashCodeBuilder().append(empId).append(lastName).append(title).append(salary).append(address).append(phoneNo).append(additionalProperties).toHashCode();
}

@Override
public boolean equals(Object other) {
    if (other == this) {
        return true;
    }
    if ((other instanceof Employee) == false) {
        return false;
    }
    Employee rhs = ((Employee) other);
    return new EqualsBuilder().append(empId, rhs.empId).append(lastName, rhs.lastName).append(title, rhs.title).append(salary, rhs.salary).append(address, rhs.address).append(phoneNo, rhs.phoneNo).append(additionalProperties, enter code hererhs.additionalProperties).isEquals();
}

}

2 地址

@JsonInclude(JsonInclude.Include.NON_NULL)
     @Generated("org.jsonschema2pojo")
    @JsonPropertyOrder({
   "city",
  "pincode",
  "landMark"
  })
public class Address {

@JsonProperty("city")
private String city;
@JsonProperty("pincode")
private Integer pincode;
@JsonProperty("landMark")
private String landMark;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String,        Object>();

/**
 * 
 * @return
 *     The city
 */
@JsonProperty("city")
public String getCity() {
    return city;
}

/**
 * 
 * @param city
 *     The city
 */
@JsonProperty("city")
public void setCity(String city) {
    this.city = city;
}

/**
 * 
 * @return
 *     The pincode
 */
@JsonProperty("pincode")
public Integer getPincode() {
    return pincode;
}

/**
 * 
 * @param pincode
 *     The pincode
 */
@JsonProperty("pincode")
public void setPincode(Integer pincode) {
    this.pincode = pincode;
}

/**
 * 
 * @return
 *     The landMark
 */
@JsonProperty("landMark")
public String getLandMark() {
    return landMark;
}

/**
 * 
 * @param landMark
 *     The landMark
 */
@JsonProperty("landMark")
public void setLandMark(String landMark) {
    this.landMark = landMark;
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

@Override
public int hashCode() {
    return new HashCodeBuilder().append(city).append(pincode).append(landMark).append(additionalProperties).toHashCode();
}

@Override
public boolean equals(Object other) {
    if (other == this) {
        return true;
    }
    if ((other instanceof Address) == false) {
        return false;
    }
    Address rhs = ((Address) other);
    return new EqualsBuilder().append(city, rhs.city).append(pincode, rhs.pincode).append(landMark, rhs.landMark).append(additionalProperties, rhs.additionalProperties).isEquals();
}

}

3.电话号码

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"mobile",
"landLine"
})
public class PhoneNo {

@JsonProperty("mobile")
private Integer mobile;
@JsonProperty("landLine")
private Integer landLine;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 * 
 * @return
 *     The mobile
 */
@JsonProperty("mobile")
public Integer getMobile() {
    return mobile;
}

/**
 * 
 * @param mobile
 *     The mobile
 */
@JsonProperty("mobile")
public void setMobile(Integer mobile) {
    this.mobile = mobile;
}

/**
 * 
 * @return
 *     The landLine
 */
@JsonProperty("landLine")
public Integer getLandLine() {
    return landLine;
}

/**
 * 
 * @param landLine
 *     The landLine
 */
@JsonProperty("landLine")
public void setLandLine(Integer landLine) {
    this.landLine = landLine;
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

@Override
public int hashCode() {
    return new HashCodeBuilder().append(mobile).append(landLine).append(additionalProperties).toHashCode();
}

@Override
public boolean equals(Object other) {
    if (other == this) {
        return true;
    }
    if ((other instanceof PhoneNo) == false) {
        return false;
    }
    PhoneNo rhs = ((PhoneNo) other);
    return new EqualsBuilder().append(mobile, rhs.mobile).append(landLine, rhs.landLine).append(additionalProperties, rhs.additionalProperties).isEquals();
}

}

提前致谢

【问题讨论】:

  • 不清楚你的问题是什么。您发布了一个 JSON Schema 并询问如何“获取 pojo 类的属性”。你的意思有十几种解释。要具体并展示您迄今为止所做的尝试。
  • 我想为我的 json 模式创建运行时 pojo 类。从 pojo 类中我想获取每个字段和类型。从这些字段中我想为 mongodb 创建查询。意味着在这种情况下,我有 Employee pojo 类,因为我嵌入了 Address 类,其中包含字段 landMark、zipcode.so 我想读取字段并将查询作为 Employee.Address.landMark
  • 不清楚您所说的“获取字段”是什么意思。在您的评论中,您说“获取字段及其类型”。这表明你想使用反射来找出你的类有哪些字段。但是您听起来也想获得某些字段的值。您是否正在尝试编写一些代码,其中输入是 Employee 类型的对象,输出是 mongodb 查询?

标签: java json


【解决方案1】:

您可以使用 java.beans.Introspector.getBeanInfo(Class) 。这里有一个使用该机制的示例:Java Beans Introspector

【讨论】:

  • 嗨@Kris 感谢您的回答。在这种情况下,我尝试使用您给定的解决方案,它的工作方式与我之前的逻辑相同。我无法获取包含的 pojo 类的属性。我只想获取字段以及包含在一个 pojo 中的 evry pojo 类的类型。还有其他方法吗?或者我们必须手动读取。
  • 你可以读取我认为的字段类型,一旦你知道你用beans Inspector挖掘得更深我猜。
  • 我尝试使用 beans Inspector,但无法获取嵌入式 pojo 类。你能举一些例子吗?谢谢
猜你喜欢
  • 2012-10-19
  • 2012-06-10
  • 2013-01-04
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多