I used to validate my beans inside my spring @RestController like:
@PostMapping("/documents")
@ResponseStatus(HttpStatus.CREATED)
fun create(@Valid @RequestBody document: DocumentCreate) {
return documentService.create(document)
}
Even it seems stupid, I would like now to validate my bean depending on a DocumentCreate boolean property like:
@PostMapping("/documents")
@ResponseStatus(HttpStatus.CREATED)
fun create(@RequestBody document: DocumentCreate) {
return when {
document.needValidation -> documentService.createWithValidation(document),
else -> documentService.createWithoutValidation(document)
}
}
I tried to move the @Valid annotation to the document @Service class but it does not trigger the validation. How can I achieve this? Is it possible?