Validate Json Request Schema

Viewed 560

So I have this Controller class that contain this method:

    @RequestMapping(value = "/x", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<MyRepsonseClass> get(
        @ApiParam(value = "x", required = true) @Valid @RequestBody MyRequestClass request
    ) throws IOException {
        //yada yada my logic here
        return something;
    }

The Json Request gets automatically Mapped to MyRequestClass.java

This is what that class looks like:

@lombok.ToString
@lombok.Getter
@lombok.Setter
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(description = "description")
public class MyRequestClass {
    private List<SomeClass> attribute1;
    private SomeOtherClass attribute2;
    private YetAnotherClass attribute3;
}

This is an example of a valid json request:

{
    "attribute1": [
        {
            "key":"value"
        }
    ],
    "attribute3": {
        "key":"value"
    }
}

Now, my requirement is to return an Error Message when the request contains an attribute that doesn't exist in the MyRequestClass.java.

As such:

{
    "attribute1": [
        {
            "key":"value"
        }
    ],
    "attribute_that_doesnt_exist": {
        "key":"value"
    }
}

Right now it's not throwing any error. Rather, it's simply not mapping that attribute to anything. Are there annotations I can utilize that can make this happen quickly ?? Thank you.

2 Answers
Related