File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed
java/ru/javawebinar/topjava Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 11package ru .javawebinar .topjava .to ;
22
3+ import javax .validation .constraints .Email ;
4+ import javax .validation .constraints .NotBlank ;
5+ import javax .validation .constraints .Size ;
6+
37public class UserTo extends BaseTo {
48
9+ @ NotBlank
10+ @ Size (min = 2 , max = 100 )
511 private String name ;
612
13+ @ Email
14+ @ NotBlank
15+ @ Size (max = 100 )
716 private String email ;
817
18+ @ NotBlank
19+ @ Size (min = 5 , max = 32 , message = "length must be between 5 and 32 characters" )
920 private String password ;
1021
1122 public UserTo () {
Original file line number Diff line number Diff line change 22
33import org .springframework .http .HttpStatus ;
44import org .springframework .http .MediaType ;
5+ import org .springframework .http .ResponseEntity ;
6+ import org .springframework .validation .BindingResult ;
57import org .springframework .web .bind .annotation .*;
68import ru .javawebinar .topjava .model .User ;
79import ru .javawebinar .topjava .to .UserTo ;
810
11+ import javax .validation .Valid ;
912import java .util .List ;
13+ import java .util .stream .Collectors ;
1014
1115@ RestController
1216@ RequestMapping (value = "/admin/users" , produces = MediaType .APPLICATION_JSON_VALUE )
@@ -32,13 +36,19 @@ public void delete(@PathVariable int id) {
3236 }
3337
3438 @ PostMapping
35- @ ResponseStatus (HttpStatus .NO_CONTENT )
36- public void createOrUpdate (UserTo userTo ) {
39+ public ResponseEntity <String > createOrUpdate (@ Valid UserTo userTo , BindingResult result ) {
40+ if (result .hasErrors ()) {
41+ String errorFieldsMsg = result .getFieldErrors ().stream ()
42+ .map (fe -> String .format ("[%s] %s" , fe .getField (), fe .getDefaultMessage ()))
43+ .collect (Collectors .joining ("<br>" ));
44+ return ResponseEntity .unprocessableEntity ().body (errorFieldsMsg );
45+ }
3746 if (userTo .isNew ()) {
3847 super .create (userTo );
3948 } else {
4049 super .update (userTo , userTo .id ());
4150 }
51+ return ResponseEntity .ok ().build ();
4252 }
4353
4454 @ Override
Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ function successNoty(text) {
7777function failNoty ( jqXHR ) {
7878 closeNoty ( ) ;
7979 failedNote = new Noty ( {
80- text : "<span class='fa fa-lg fa-exclamation-circle'></span> Error status: " + jqXHR . status ,
80+ text : "<span class='fa fa-lg fa-exclamation-circle'></span> Error status: " + jqXHR . status + ( jqXHR . responseJSON ? "<br>" + jqXHR . responseJSON : "" ) ,
8181 type : "error" ,
8282 layout : "bottomRight"
8383 } ) ;
You can’t perform that action at this time.
0 commit comments