Skip to content

Commit 604fc35

Browse files
committed
Javax Validation
1 parent 9abd713 commit 604fc35

File tree

4 files changed

+16
-28
lines changed

4 files changed

+16
-28
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
67
<groupId>org.springframework.boot</groupId>
78
<artifactId>spring-boot-starter-parent</artifactId>
89
<version>2.4.3</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10+
<relativePath /> <!-- lookup parent from repository -->
1011
</parent>
1112
<groupId>com.itacademy</groupId>
1213
<artifactId>CrudEmpleats</artifactId>
@@ -17,6 +18,10 @@
1718
<java.version>15</java.version>
1819
</properties>
1920
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-validation</artifactId>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.springframework.boot</groupId>
2227
<artifactId>spring-boot-starter-thymeleaf</artifactId>

src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import javax.validation.Valid;
56

67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.http.HttpStatus;
@@ -22,7 +23,7 @@ public class ControllerEmployees {
2223
// Crear nou empleat
2324
@PostMapping( "/EmployeeList")
2425
@ResponseStatus(HttpStatus.CREATED) // 201
25-
public void addEmployee(@RequestBody Employee employee) {
26+
public void addEmployee(@Valid @RequestBody Employee employee) {
2627
repositori.addEmployee(employee);
2728

2829
}
@@ -42,7 +43,7 @@ public Employee getFirstEmployee(@PathVariable("id") int id) {
4243
// Actualitzar Empleat
4344
@PutMapping("/EmployeeList/{id}")
4445
@ResponseStatus(HttpStatus.ACCEPTED) // 202
45-
public Employee updateEmployee(@RequestBody Employee employee, @PathVariable("id") int id) {
46+
public Employee updateEmployee(@Valid @RequestBody Employee employee, @PathVariable("id") int id) {
4647
repositori.updateEmployee(employee,id);
4748
return repositori.getEmployeeById(id);
4849
}

src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.itacademy.CrudEmpleats.domain;
22

3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotNull;
5+
36
public class Employee {
47

8+
@NotNull
59
private int id;
10+
@NotBlank
611
private String name;
12+
@NotBlank
713
private String job;
814
private double salary ;
915

src/main/resources/templates/ModificarEmpleat.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)