77import lk .ijse .dep .note .service .exception .NotFoundException ;
88import org .springframework .beans .factory .annotation .Autowired ;
99import org .springframework .http .HttpStatus ;
10+ import org .springframework .validation .Errors ;
1011import org .springframework .validation .annotation .Validated ;
1112import org .springframework .web .bind .annotation .*;
1213import org .springframework .web .server .ResponseStatusException ;
@@ -21,14 +22,17 @@ public class UserController {
2122
2223 @ ResponseStatus (HttpStatus .CREATED )
2324 @ PostMapping (consumes = "application/json" , produces = "application/json" )
24- public UserDTO registerUser (@ RequestBody @ Valid UserDTO user ) {
25+ public UserDTO registerUser (@ RequestBody @ Validated UserDTO user , Errors errors ) {
2526
27+ if (errors .hasFieldErrors ()){
28+ throw new ResponseStatusException (HttpStatus .BAD_REQUEST ,errors .getFieldErrors ().get (0 ).getDefaultMessage ());
29+ }
2630 userService .registerUser (user );
2731
2832 return user ;
2933 }
3034
31- @ GetMapping (path = "{userId:[A-Fa-f0-\\ ~ ]{36} }}" , produces = "application/json" )
35+ @ GetMapping (path = "/ {userId:[A-Fa-f0-9 \\ - ]{36}}" , produces = "application/json" )
3236 public UserDTO getUserInfo (@ PathVariable String userId ) {
3337 System .out .println ("get" + userId );
3438
@@ -37,18 +41,20 @@ public UserDTO getUserInfo(@PathVariable String userId) {
3741 }
3842
3943 @ ResponseStatus (HttpStatus .NO_CONTENT )
40- @ DeleteMapping ("{userId:[A-Fa-f0-\\ ~ ]{36}}" )
44+ @ DeleteMapping ("/ {userId:[A-Fa-f0-9 \\ - ]{36}}" )
4145 public void deleteuser (@ PathVariable String userId ) {
4246
4347 userService .deleteUser (userId );
4448
4549 }
4650
4751 @ ResponseStatus (HttpStatus .NO_CONTENT )
48- @ PatchMapping (path = "{userId:[A-Fa-f0-\\ ~]{36}}" , consumes = "application/json" )
49- public void updateUser (@ PathVariable String userId , @ RequestBody @ Valid UserDTO user ) {
50-
52+ @ PatchMapping (path = "/{userId:[A-Fa-f0-9\\ -]{36}}" , consumes = "application/json" )
53+ public void updateUser (@ PathVariable String userId , @ RequestBody @ Valid UserDTO user , Errors errors ) {
5154
55+ if (errors .hasFieldErrors ()){
56+ throw new ResponseStatusException (HttpStatus .BAD_REQUEST ,errors .getFieldErrors ().get (0 ).getDefaultMessage ());
57+ }
5258
5359 user .setId (userId );
5460 userService .updateUser (user );
0 commit comments