0

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

user1796624
  • 3,665
  • 7
  • 38
  • 65

1 Answers1

0

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

Difference between @Valid and @Validated in Spring

Harsh
  • 812
  • 1
  • 10
  • 23
  • First thanks for your answer. API is not returning a response "Additional parameters/properties found", request are executed with no problem and additional body params are not taken in to consideration at all. Now validation comes after the request is deseriliased so I think this is not useful for me. – user1796624 Feb 10 '22 at 12:46