Java 如何从动态生成的pojo类中获取属性
我正在使用jsonschema2pojo maven插件v0.4.7从JSON模式生成POJO类。我想获取动态生成的POJO类的属性。POJO类包含在另一个POJO类中。是否有从包含onather的POJO类读取字段的解决方案?例如,在本例中,我有Employee pojo,因为有一个Address对象我想获取这两个类的字段 下面是我的模式Java 如何从动态生成的pojo类中获取属性,java,json,Java,Json,我正在使用jsonschema2pojo maven插件v0.4.7从JSON模式生成POJO类。我想获取动态生成的POJO类的属性。POJO类包含在另一个POJO类中。是否有从包含onather的POJO类读取字段的解决方案?例如,在本例中,我有Employee pojo,因为有一个Address对象我想获取这两个类的字段 下面是我的模式 { "$schema": "http://json-schema.org/draft-04/schema#", "type":
{
"$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();
}
}
提前感谢您可以使用java.beans.Introspector.getBeanInfoClass。下面是该机制的一个示例用法:不清楚您的问题是什么。发布JSON模式并询问如何获取pojo类的属性。你的意思有很多种解释。请具体说明您迄今为止所做的尝试。请看一看我想为我的json模式创建运行时pojo类。从pojo类中,我想获取每个字段和类型。从这些字段中,我想为mongodb创建查询。这意味着在本例中,我有Employee pojo类,因为我有一个嵌入了字段landMark的Address类,zipcode。所以我想读取字段并作为Employee.Address.landMark进行查询。不清楚fetch字段是什么意思。在您的评论中,您说的是fetch字段及其类型。这表示您希望使用反射来确定类具有哪些字段。但听起来你也希望得到一些字段的值。您是否试图编写一些代码,其中输入是Employee类型的对象,输出是mongodb查询?您好@Kris,谢谢您的回答。我尝试了您给定的解决方案,在这种情况下,其工作原理与我以前的逻辑相同。我无法获取包含的pojo类的属性。我只想从evry pojo类中获取字段和类型,该类包含在一个pojo中。还有其他方法吗?或者我们必须手动读取。我想,一旦您知道您使用beans Inspector进行了更深入的挖掘,您就可以读取字段的类型。我尝试使用beans Inspector,但无法获取嵌入的pojo类。你能举一些例子吗?谢谢