So, I have this controller class that contains 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 {
return something;
}
Json request is automatically mapped to MyRequestClass.java
Here's what this 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 does not exist in MyRequestClass.java.
In this way:
{
"attribute1": [
{
"key":"value"
}
],
"attribute_that_doesnt_exist": {
"key":"value"
}
}
Now he is not throwing any mistakes. Rather, it simply does not match this attribute with anything. Are there any annotations I can use so that this can happen quickly? Thank.
adbar source
share