这是我的类,它扩展了HashMap,因为响应JSON中有很多动态字段,但教育和工作是固定的,我希望那些应该与PeopleEducation和PeopleEmployment clasess一起映射
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({ "_education", "_employment" })
public class People extends HashMap<String, Object> implements Map<String, Object> {
/**
*
*/
private static final long serialVersionUID = 1L;
@JsonProperty("_education")
private List<PeopleEducation> Education = new ArrayList<PeopleEducation>();
@JsonProperty("_employment")
private List<PeopleEmployment> Employment = new ArrayList<PeopleEmployment>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return The Education
*/
@JsonProperty("_education")
public List<PeopleEducation> getEducation() {
return Education;
}
/**
*
* @param Education
* The _education
*/
@JsonProperty("_education")
public void setEducation(List<PeopleEducation> Education) {
this.Education = Education;
}
/**
*
* @return The Employment
*/
@JsonProperty("_employment")
public List<PeopleEmployment> getEmployment() {
return Employment;
}
/**
*
* @param Employment
* The _employment
*/
@JsonProperty("_employment")
public void setEmployment(List<PeopleEmployment> Employment) {
this.Employment = Employment;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
因此可以使用Spring Rest Template
将这些字段映射到特定的类ResponseEntity<People> response = restClient.getResource(url, headerMap,People.class);
答案 0 :(得分:1)
默认情况下,如果它是有效的JSON,它将自动匹配。 如果响应是字符串格式,则必须使用ObjectMapper 解析并将其转换为Object。
为了更好地理解,你还可以发布你的JSON字符串。