我正在使用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();
}
}
在此先感謝
目前尚不清楚你的問題是什麼。你發佈了一個JSON Schema,並詢問如何「獲取POJO類的屬性」。對於你的意思有十幾種解釋。具體一些,並顯示你到目前爲止所嘗試過的。 – lexicore
看看http://stackoverflow.com/questions/32226110/loading-a-map-using-properties-class/32226264#32226264 –
我想爲我的json schema.from pojo類創建運行時pojo類我想要獲取每個字段和那裏types.from我想爲mongodb創建查詢的字段。意味着在這種情況下,我有員工POJO類,我有地址類嵌入字段landMark,zipcode.so我想閱讀字段和查詢作爲Employee.Address.landMark –