[JAVA] Ignore parameters that do not exist in the model with ObjectMapper # readValue

A memo that I was addicted to when receiving a response in json format from an external API and converting it to a model class.

What are you addicted to?

I am converting json to a model with com.fasterxml.jackson.databind.ObjectMapper # readValue (String, TypeReference), but an exception occurs if there is an item in json that does not exist in the model field.

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

Solution

Add @JsonIgnoreProperties (ignoreUnknown = true) to the model class.

@NoArgsConstructor
@JsonNaming(SnakeCaseStrategy.class)
@JsonAutoDetect(getterVisibility = Visibility.NONE)
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class hogehoge {
}

Like this.

Chan-chan.

Recommended Posts

Ignore parameters that do not exist in the model with ObjectMapper # readValue
[Gradle] The story that the class file did not exist in the jar file
Ebean.update () is not executed in the inherited model.
Processing when an ID that does not exist in the database is entered in the URL
In Ruby, methods with? Do not always return true/false.
Memo that transitions to the login screen if you are not logged in with devise
[Development log ⑬] Do not let 0 be entered in the form !!
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
Solved the problem that the scroll event did not fire in JQuery
Handling when calling a key that does not exist in hash
What to do when the changes in the Servlet are not reflected
Problem that the attribute of User model becomes nil in ActionMailer
Programs that use io_uring do not work with Docker on CentOS 8
[Swift] Variable shapes that were not taught in the introductory book