Β© 2026 Hedgehog Software, LLC
import jakarta.validation.constraints.*; data class RequestBodyDTO( @NotBlank(message = "ID is required.") val iD: String )
import jakarta.validation.Valid import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @RestController() @RequestMapping("/foo") class Handler { @PostMapping fun handle(@Valid @RequestBody requestBody: RequestBodyDTO): ResponseEntity<Unit> { return ResponseEntity.ok().build() } }
@Valid
import org.springframework.http.HttpStatus import org.springframework.validation.FieldError import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.annotation.* @ControllerAdvice class ValidationExceptionHandler { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException::class) fun handleValidationException( exception: MethodArgumentNotValidException ): Map<String, String?> { return exception.bindingResult.allErrors.associate { error -> val fieldName = (error as FieldError).field val errorMessage = error.defaultMessage fieldName to errorMessage } } }
ValidationExceptionHandler