File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed
src/main/java/com/example/springbootapirest Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 2727 <artifactId >h2</artifactId >
2828 <scope >runtime</scope >
2929 </dependency >
30+ <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
31+ <dependency >
32+ <groupId >org.springframework.boot</groupId >
33+ <artifactId >spring-boot-starter-validation</artifactId >
34+ <version >2.7.1</version >
35+ </dependency >
36+
3037 <dependency >
3138 <groupId >org.springframework.boot</groupId >
3239 <artifactId >spring-boot-starter-test</artifactId >
Original file line number Diff line number Diff line change 55import org .springframework .beans .factory .annotation .Autowired ;
66import org .springframework .web .bind .annotation .*;
77
8+ import javax .validation .Valid ;
89import java .util .List ;
910import java .util .Optional ;
1011
@@ -16,7 +17,7 @@ public class DepartmentController {
1617 private DepartmentService departmentService ;
1718
1819 @ PostMapping ("/departments" )
19- public Department saveDepartment (@ RequestBody Department department ) {
20+ public Department saveDepartment (@ Valid @ RequestBody Department department ) {
2021 return departmentService .saveDepartment (department );
2122 }
2223
Original file line number Diff line number Diff line change 11package com .example .springbootapirest .entity ;
22
3+ import org .hibernate .validator .constraints .Length ;
4+
35import javax .persistence .Entity ;
46import javax .persistence .GeneratedValue ;
57import javax .persistence .GenerationType ;
68import javax .persistence .Id ;
9+ import javax .validation .constraints .*;
710
811@ Entity
912public class Department {
1013
1114 @ Id
1215 @ GeneratedValue (strategy = GenerationType .AUTO )
1316 private Long departmentId ;
17+
18+ /*@Email
19+ @Future
20+ @FutureOrPresent
21+ @Past // VALIDATION EXAMPLES
22+ @Length(max = 5, min = 1)
23+ @Size(max = 15, min = 1)
24+ @PositiveOrZero
25+ ... */
26+ @ NotBlank (message = "Debes añadir un nombre de departamento" )
1427 private String departmentName ;
1528 private String departmentAddress ;
1629 private String departmentCode ;
You can’t perform that action at this time.
0 commit comments