JAVA-30670 Improvement on Spring Validation article (#16023)
This commit is contained in:
parent
d8847225cd
commit
e4b249c784
@ -1,6 +1,9 @@
|
|||||||
package com.baeldung.spring.servicevalidation.controller;
|
package com.baeldung.spring.servicevalidation.controller;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintViolation;
|
||||||
|
import jakarta.validation.ConstraintViolationException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -15,8 +18,12 @@ public class UserAccountController {
|
|||||||
private UserAccountService service;
|
private UserAccountService service;
|
||||||
|
|
||||||
@PostMapping("/addUserAccount")
|
@PostMapping("/addUserAccount")
|
||||||
public Object addUserAccount(@RequestBody UserAccount userAccount) {
|
public ResponseEntity<?> addUserAccount(@RequestBody UserAccount userAccount) {
|
||||||
return service.addUserAccount(userAccount);
|
try {
|
||||||
|
return ResponseEntity.ok(service.addUserAccount(userAccount));
|
||||||
|
} catch(ConstraintViolationException e) {
|
||||||
|
return ResponseEntity.badRequest().body(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user