Reputation: 395
So in this case, I want to consume a third party API to compare capitals with countries, I don't need any other attribute from the Json but those 2, however the API I'm using has a lot of extra attributes on it.
What is the easiest way to deserialize the Json into a class with just those 2 attributes I want?
I tried this but of course it didn't work:
Country country = restTemplate.getForObject( "https://restcountries.eu/rest/v2/capital/"+learning.getCapital(), Country.class);
I understand that this doesn't work because it's trying to map the Json attributes into the class which of course lacks the remaining attributes (It just has two string attributes called Name and Capital).
Upvotes: 0
Views: 866
Reputation: 14712
if you want this feature globally you can set in your application.properties
spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=false
Upvotes: 1
Reputation: 38132
Annotate the class with @JsonIgnoreProperties:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
Upvotes: 1
Reputation: 3572
You could ignore unkown properties on object mapper for this rest template:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Upvotes: 1