'Spring boot rest api, fail on unknown property in the body request
I have a spring-boot rest api service.
Just for one of the rest POST api calls I would like to make sure that no additional properties are send in the request body object. I have realised there is a way to use a custom objectMapper that will fail deserialisation in this kind of case but I have a bit of problem setting up an object mapper just for the particular call. What would be the best approach, pls write/extend my example.
My example:
PostMapping("/")
public static ResponseEntity apiCallNeedsToFailInCaseOfAdditionalPropertiesInBody(@RequestBody body){
/// some code
}
Thanks
Solution 1:[1]
As far as I know, if you are trying to hit API via Postman, it will automatically give you a result with error message saying "Additional parameters/properties found" or something similar in error response when you passed additional parameter(s).
Your solution will also work, but no need for the same.
Instead, when you have endpoints growing in your application, you can make use of @Valid and @Validated annotations to take care of that request body parameter validations.
Check if this article throws some light around your query
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh |
