Skip to content

Commit 37eb7e9

Browse files
committed
Added Hibernate validation
1 parent 0497e8c commit 37eb7e9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
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>

src/main/java/com/example/springbootapirest/controller/DepartmentController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.web.bind.annotation.*;
77

8+
import javax.validation.Valid;
89
import java.util.List;
910
import 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

src/main/java/com/example/springbootapirest/entity/Department.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package com.example.springbootapirest.entity;
22

3+
import org.hibernate.validator.constraints.Length;
4+
35
import javax.persistence.Entity;
46
import javax.persistence.GeneratedValue;
57
import javax.persistence.GenerationType;
68
import javax.persistence.Id;
9+
import javax.validation.constraints.*;
710

811
@Entity
912
public 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;

0 commit comments

Comments
 (0)