請求阿里雲的OSS接口圖片信息,返回json格式的數據,經過ObjectMapper將json轉爲Image對象時候報錯轉換失敗java
將json轉對象的代碼:json
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);
報錯信息:app
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)
Image對象中缺乏json的某個字段屬性引發阿里雲
解決:spa
一、加上以下兩行代碼:.net
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
二、在須要轉化的對象的類中添加註解,註解信息以下: orm
@JsonIgnoreProperties(ignoreUnknown = true)xml
參考:對象
https://stackoverflow.com/questions/51034173/backblazetokenresponse-com-fasterxml-jackson-databind-exc-unrecognizedpropertyblog
https://blog.csdn.net/qq_30739519/article/details/51273544