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:
- Springboot is compatible with jackson.databind and reports errors [How to Solve]
- @Jsonignoreproperties does not work [How to Solve]
- com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class tonels.
- [Solved] com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): ha
- [Solved] Java.io.EOFException: No content to map to Obje…
- [How to Solve] jackson not marked as ignorable
- [Solved] Interpreter error /com.fasterxml.jackson.databind.JavaType
- [Solved] swagger Document Error: For input string: “”
- Postman passing Josn field error: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
- [Solved] No serializer found for class XXX and no properties discovered to create BeanSerializer