2015-10-08 46 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类。你能举一些例子吗?谢谢 –