9

Jackson JSON Deserialization – UnrecognizedPropertyException

 4 years ago
source link: https://www.briansdevblog.com/2019/11/jackson-json-deserialization-unrecognizedpropertyexception/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Whats the Problem?

I ran into a UnrecognizedPropertyExceptionUnrecognizedPropertyException today trying to deserialize the following JSON.

{
  "Places": [{
    "PlaceId": "LOND-sky",
    "PlaceName": "London",
    "CountryId": "UK-sky",
    "RegionId": "",
    "CityId": "LOND-sky",
    "CountryName": "United Kingdom"
  }, {
    "PlaceId": "LHR-sky",
    "PlaceName": "London Heathrow",
    "CountryId": "UK-sky",
    "RegionId": "",
    "CityId": "LOND-sky",
    "CountryName": "United Kingdom"
  }]
}
  1. "Places": [{
  2. "PlaceId": "LOND-sky",
  3. "PlaceName": "London",
  4. "CountryId": "UK-sky",
  5. "RegionId": "",
  6. "CityId": "LOND-sky",
  7. "CountryName": "United Kingdom"
  8. "PlaceId": "LHR-sky",
  9. "PlaceName": "London Heathrow",
  10. "CountryId": "UK-sky",
  11. "RegionId": "",
  12. "CityId": "LOND-sky",
  13. "CountryName": "United Kingdom"
{
  "Places": [{
    "PlaceId": "LOND-sky",
    "PlaceName": "London",
    "CountryId": "UK-sky",
    "RegionId": "",
    "CityId": "LOND-sky",
    "CountryName": "United Kingdom"
  }, {
    "PlaceId": "LHR-sky",
    "PlaceName": "London Heathrow",
    "CountryId": "UK-sky",
    "RegionId": "",
    "CityId": "LOND-sky",
    "CountryName": "United Kingdom"
  }]
}

The PlacesPlaces model was defined as

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Places {

  private List<Place> places;
}
  1. import lombok.Getter;
  2. import lombok.Setter;
  3. @Setter
  4. @Getter
  5. public class Places {
  6. private List<Place> places;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Places {

  private List<Place> places;
}

and the PlacePlace model was defined as

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Place {

  String placeId;
  String placeName;
  String countryId;
  String regionId;
  String cityId;
  String countryName;
}
  1. import lombok.Getter;
  2. import lombok.Setter;
  3. import lombok.ToString;
  4. @Getter
  5. @Setter
  6. @ToString
  7. public class Place {
  8. String placeId;
  9. String placeName;
  10. String countryId;
  11. String regionId;
  12. String cityId;
  13. String countryName;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Place {

  String placeId;
  String placeName;
  String countryId;
  String regionId;
  String cityId;
  String countryName;
}

I was using a simplke ObjectMapperObjectMapper like this.

ObjectMapper objectMapper = new ObjectMapper();
Places places = objectMapper.readValue(json, Places.class);
  1. ObjectMapper objectMapper = new ObjectMapper();
  2. Places places = objectMapper.readValue(json, Places.class);
ObjectMapper objectMapper = new ObjectMapper();
Places places = objectMapper.readValue(json, Places.class);

The  UnrecognizedPropertyExceptionUnrecognizedPropertyException stacktrace was as follows.

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Places" (class com.briansdevblog.model.Places), not marked as ignorable (one known property: "placesX"])
 at [Source: (String)"{"Places":[{"PlaceId":"LOND-sky","PlaceName":"London","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LHR-sky","PlaceName":"London Heathrow","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LGW-sky","PlaceName":"London Gatwick","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"STN-sky","PlaceName":"London Stansted","CountryId":"UK-sky","RegionId":""[truncated 855 chars]; line: 1, column: 12] (through reference chain: com.briansdevblog.model.Places["Places"])
  at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
  at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
  1. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Places" (class com.briansdevblog.model.Places), not marked as ignorable (one known property: "placesX"])
  2. at [Source: (String)"{"Places":[{"PlaceId":"LOND-sky","PlaceName":"London","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LHR-sky","PlaceName":"London Heathrow","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LGW-sky","PlaceName":"London Gatwick","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"STN-sky","PlaceName":"London Stansted","CountryId":"UK-sky","RegionId":""[truncated 855 chars]; line: 1, column: 12] (through reference chain: com.briansdevblog.model.Places["Places"])
  3. at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
  4. at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
  5. at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
  6. at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
  7. at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
  8. at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
  9. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
  10. at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
  11. at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Places" (class com.briansdevblog.model.Places), not marked as ignorable (one known property: "placesX"])
 at [Source: (String)"{"Places":[{"PlaceId":"LOND-sky","PlaceName":"London","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LHR-sky","PlaceName":"London Heathrow","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"LGW-sky","PlaceName":"London Gatwick","CountryId":"UK-sky","RegionId":"","CityId":"LOND-sky","CountryName":"United Kingdom"},{"PlaceId":"STN-sky","PlaceName":"London Stansted","CountryId":"UK-sky","RegionId":""[truncated 855 chars]; line: 1, column: 12] (through reference chain: com.briansdevblog.model.Places["Places"])
  at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
  at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)

Solution

If you look at the JSON sample above, you’ll see that the attributes are uppercased while the member variables on the PlacePlace and PlacesPlaces models are lowercased. I don’t have control of the API so I can’t change the case of the JSON attributes. As a workaround, you can configure the ObjectMapperObjectMapper to tell Jackson to accept case insensitive properties as follows.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
  1. ObjectMapper objectMapper = new ObjectMapper();
  2. objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK