0

I would like to validate both the @RequestBody as well as path parameters. Ideally, I'd like to create my own @CustomValid annotation instead of the @Valid one.

In addition to doing checks like @NotNull on the DTO itself, I want to actually check if say, the id of the object to be updated actually exists in the database. For example for this request:

{
  "id": "2348291983918",
  "name": "Carol"
}

I'd like to do a lookup for whether the users mongo collection actually contains an object with the id passed in or reject it.

Does anyone know how to do this?

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
demig0d
  • 1,131
  • 1
  • 12
  • 24
  • Rather than using validators for this, I usually do the DB lookup in my code, and then throw an exception if the ID doesn't exist. Then I use `@ExceptionHandler` to map that exception to a 404 error. – g00glen00b Aug 06 '16 at 19:07
  • An Easy why could be perform a method which starts by 'is', add annotation @assertTrue and return a boolean, then validation checks it. your method could be as next exmaple: public boolean isCorrectId(){ if(yourrepository.findbyId(id) != null) return true; else return false; }. More info: http://stackoverflow.com/a/13012400/4751165 – Pau Aug 06 '16 at 20:41
  • Otherwise validate your pathvariable it's not possible at the moment: https://jira.spring.io/browse/SPR-6380 You must do it programmatically – Pau Aug 06 '16 at 20:45

0 Answers0