JAVA-30670 Improvement on Spring Validation article (#16023)

This commit is contained in:
Amit Pandey 2024-03-04 23:36:14 +05:30 committed by GitHub
parent d8847225cd
commit e4b249c784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 2 deletions

View File

@ -1,6 +1,9 @@
package com.baeldung.spring.servicevalidation.controller;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -15,8 +18,12 @@ public class UserAccountController {
private UserAccountService service;
@PostMapping("/addUserAccount")
public Object addUserAccount(@RequestBody UserAccount userAccount) {
return service.addUserAccount(userAccount);
public ResponseEntity<?> addUserAccount(@RequestBody UserAccount userAccount) {
try {
return ResponseEntity.ok(service.addUserAccount(userAccount));
} catch(ConstraintViolationException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
}