2015-10-08 76 views
0

我正在使用jsonschema2pojo-maven-plugin v0.4.7從JSON模式生成POJO類。在那裏我想獲取動態生成的pojo類的屬性。pojo類包含onather pojo類。是否有解決方案來閱讀包含onather的pojo類的字段?例如在這種情況下,我有Employee pojo,因爲有Address對象我想要獲取這兩個類的字段。如何從動態生成pojo類中獲取屬性

下面

是我的架構

{ 
    "$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(); 
} 

}

在此先感謝

+0

目前尚不清楚你的問題是什麼。你發佈了一個JSON Schema,並詢問如何「獲取POJO類的屬性」。對於你的意思有十幾種解釋。具體一些,並顯示你到目前爲止所嘗試過的。 – lexicore

+0

看看http://stackoverflow.com/questions/32226110/loading-a-map-using-properties-class/32226264#32226264 –

+0

我想爲我的json schema.from pojo類創建運行時pojo類我想要獲取每個字段和那裏types.from我想爲mongodb創建查詢的字段。意味着在這種情況下,我有員工POJO類,我有地址類嵌入字段landMark,zipcode.so我想閱讀字段和查詢作爲Employee.Address.landMark –

回答

0

您可以使用java.beans.Introspector.getBeanInfo(Class)。這裏有一個示例使用該機制:Java Beans Introspector

+0

嗨@Kris感謝您的答案。我試着用你的給定在這種情況下,它的解決方案與我以前的邏輯相同。我無法獲取包含pojo類的屬性。我只想從evry pojo類中獲取字段和類型,其中包含一個pojo。是否還有其他方法?或者我們必須手動閱讀。 –

+0

您可以閱讀我認爲的字段類型,一旦您知道您使用Bean Inspector進行深入挖掘,我就會猜到。 – Kris

+0

我嘗試過使用Bean Inspector,但我無法獲取嵌入的Pojo類。你能舉一些例子嗎?謝謝 –