0

I'm getting a JSON response in a RESTful endpoint. The fields in the response are variable. In particular some fields that I do not care about them. Because of some requirements I have to use JSR 303 validation annotation to an envelope class to handle the response.

The body of the response is like:

{ "parameter1":"val1", "parameter2":"val2", "optional_parameter":"valopt", "not_important_list":["v1","v2","v3"] }

My class is like:

public class MessageEnvelope {
    @NotNull
    @NotBlank
    public final String parameter1;

    @NotNull
    @NotBlank
    public final String parameter2;

    //the rest of the fields should be ignored
}

I receive mapping error because of the the extra fields. How can I ignore the extra fields that I do not care about them?

A F
  • 53
  • 1
  • 7
  • 1
    The mapping error sounds like it's in the JSON -> Object library you're using (e.g. Jackson, GSON). I don't think it's related to validation. I'm most familiar with Jackson and there are some straight forward ways to tell it to ignore unrecognized fields seen in JSON. – Jay Anderson Jul 07 '16 at 20:24
  • 1
    Correct. This comment helped me to figure out the issue. In my case the deserialization is done with Jackson and I could ignore them with `@JsonIgnoreProperties(ignoreUnknown=true)` annotation before the class definition. – A F Jul 07 '16 at 20:39

1 Answers1

0

The mapping error was actually in the JSON -> Object library. In this case "Jackson".

I could solve that with the @JsonIgnoreProperties(ignoreUnknown=true) annotation before the class definition.

@JsonIgnoreProperties(ignoreUnknown=true)
public class MessageEnvelope { 
    ...
}
A F
  • 53
  • 1
  • 7