[Solved] com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “FileSi…

Request the image information of the OSS interface of alicloud, return the data in JSON format, and report an error when converting JSON to image object through objectmapper. The conversion fails

the code to convert JSON to object:

String jsonStr = "{\n" +
        "    \"FileSize\": {\"value\": \"25929\"},\n" +
        "    \"Format\": {\"value\": \"jpg\"},\n" +
        "    \"ImageHeight\": {\"value\": \"200\"},\n" +
        "    \"ImageWidth\": {\"value\": \"300\"},\n" +
        "    \"ResolutionUnit\": {\"value\": \"1\"},\n" +
        "    \"XResolution\": {\"value\": \"100/1\"},\n" +
        "    \"YResolution\": {\"value\": \"100/1\"}}";
ObjectMapper objectMapper = new ObjectMapper();
Image image = objectMapper.readValue(jsonStr, Image.class);

Error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FileSize" (class com.xxx.xxx.bean.Image), not marked as ignorable (7 known properties: "resolutionUnit", "imageHeight", "xresolution", "yresolution", "format", "imageWidth", "fileSize"])
 at [Source: {
    "FileSize": {"value": "25929"},
    "Format": {"value": "jpg"},
    "ImageHeight": {"value": "200"},
    "ImageWidth": {"value": "300"},
    "ResolutionUnit": {"value": "1"},
    "XResolution": {"value": "100/1"},
    "YResolution": {"value": "100/1"}}; line: 2, column: 18] (through reference chain: com.xxx.xxx.bean.Image["FileSize"])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:834)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1093)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1489)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1467)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:282)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
	at com.xxx.xxx.service.PostOssService.main(OssService.java:103)

Reason:

The lack of a field attribute of JSON in the image object caused

solution:

1. Add the following two lines:

objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));

2. Add a comment to the class of the object that needs to be transformed. The comment information is as follows:

@JsonIgnoreProperties(ignoreUnknown = true)

Similar Posts: