![](/img/trans.png)
[英]How to resolve exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException
[英]Jackson parsing error: exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “Results”
有一些这样的主题,但是我已经读完了,仍然没有运气。
我有一个要反序列化来自Web服务的JSON
响应的类。 简而言之,我花了太多时间研究这个问题,希望有人能找出我的方法中的错误。 按照标题,我正在使用Jackson
库。
以下课程的摘录:
final class ContentManagerResponse implements Serializable {
@JsonProperty("Results")
private List<OrgSearchResult> results = null;
@JsonProperty("PropertiesAndFields")
private PropertiesAndFields propertiesAndFields;
@JsonProperty("TotalResults")
private Integer totalResults;
@JsonProperty("CountStringEx")
private String countStringEx;
@JsonProperty("MinimumCount")
private Integer minimumCount;
@JsonProperty("Count")
private Integer count;
@JsonProperty("HasMoreItems")
private Boolean hasMoreItems;
@JsonProperty("SearchTitle")
private String searchTitle;
@JsonProperty("HitHighlightString")
private String hitHighlightString;
@JsonProperty("TrimType")
private String trimType;
@JsonProperty("ResponseStatus")
private ResponseStatus responseStatus;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("Results")
public List<OrgSearchResult> getResults() {
return results;
}
@JsonProperty("Results")
public void setResults(List<OrgSearchResult> results) {
this.results = results;
}
//additional getters and setters.
如前所述, Results
是似乎有错误的属性。
JSON响应如下。
{
"Results": [
{
"TrimType": "Location",
"Uri": 1684
}
],
"PropertiesAndFields": {},
"TotalResults": 1,
"CountStringEx": "1 Location",
"MinimumCount": 1,
"Count": 0,
"HasMoreItems": false,
"SearchTitle": "Locations - type:Organization and id:24221",
"HitHighlightString": "",
"TrimType": "Location",
"ResponseStatus": {}
}
我正在使用同一类反序列化以下响应,并且可以正常工作:
{
"Results": [
{
"LocationIsWithin": {
"Value": true
},
"LocationSortName": {
"Value": "GW_POS_3"
},
"LocationTypeOfLocation": {
"Value": "Position",
"StringValue": "Position"
},
"LocationUserType": {
"Value": "RecordsWorker",
"StringValue": "Records Co-ordinator"
},
"TrimType": "Location",
"Uri": 64092
}
],
"PropertiesAndFields": {},
"TotalResults": 1,
"MinimumCount": 0,
"Count": 0,
"HasMoreItems": false,
"TrimType": "Location",
"ResponseStatus": {}
}
错误消息只是误导吗? 除了第二个(有效)有效负载外,该结构相同,但类中不包含某些字段。 我希望这会出错。
对于它的价值,我还包括以下OrgSearchResult
类:
final class OrgSearchResult implements Serializable {
@JsonProperty("TrimType") private String trimType;
@JsonProperty("Uri") private String uri;
@JsonIgnore private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
很多故障排除。 我什至尝试使用忽略属性似乎无法使它们正常工作。
完整错误:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“ Results”(sailpoint.doet.contentmanager.ContentManagerResponse类),在[Source:java.io.StringReader@5c6648b0;上未标记为可忽略。 第1行,第13列](通过参考链:sailpoint.doet.contentmanager.ContentManagerResponse [“ Results”])
您可以使用PropertyNamingStrategy.UPPER_CAMEL_CASE策略来提高POJO
类的可读性。 另外,您可以使用JsonAnySetter
批注读取所有其他属性。 以下示例显示了模型的外观:
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
System.out.println(mapper.readValue(jsonFile, ContentManagerResponse.class));
}
}
class ContentManagerResponse {
private List<OrgSearchResult> results;
private Map<String, Object> propertiesAndFields;
private Integer totalResults;
private String countStringEx;
private Integer minimumCount;
private Integer count;
private Boolean hasMoreItems;
private String searchTitle;
private String hitHighlightString;
private String trimType;
private Map<String, Object> responseStatus;
// getters, setters, toString
}
class OrgSearchResult {
private String trimType;
private String uri;
private Map<String, Object> additionalProperties = new HashMap<>();
@JsonAnySetter
public void additionalProperties(String name, Object value) {
additionalProperties.put(name, value);
}
// getters, setters, toString
}
对于上面的代码,第一个JSON
负载打印:
ContentManagerResponse{results=[OrgSearchResult{trimType='Location', uri='1684', additionalProperties={}}], propertiesAndFields={}, totalResults=1, countStringEx='1 Location', minimumCount=1, count=0, hasMoreItems=false, searchTitle='Locations - type:Organization and id:24221', hitHighlightString='', trimType='Location', responseStatus='{}'}
对于上面的代码,第二个JSON
负载打印:
ContentManagerResponse{results=[OrgSearchResult{trimType='Location', uri='64092', additionalProperties={LocationSortName={Value=GW_POS_3}, LocationUserType={Value=RecordsWorker, StringValue=Records Co-ordinator}, LocationIsWithin={Value=true}, LocationTypeOfLocation={Value=Position, StringValue=Position}}}], propertiesAndFields={}, totalResults=1, countStringEx='null', minimumCount=0, count=0, hasMoreItems=false, searchTitle='null', hitHighlightString='null', trimType='Location', responseStatus='{}'}
您不需要实现Serializable
接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.