如何从动态生成的pojo类中获取属性

时间:2015-10-08 06:12:34

标签: java json

我正在使用jsonschema2pojo-maven-plugin v0.4.7从JSON模式生成POJO类。我想要获取动态生成的pojo类的属性。包含onather pojo类的pojo类。是否有从包含onather的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.Employee

 @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.phoneNo

@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();
}

}

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用java.beans.Introspector.getBeanInfo(Class)。您可以在此处使用该机制的示例:Java Beans Introspector