From 640d1313f9fd5d183487cf43132c7e919aebf861 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 2 Jan 2024 22:12:49 +0100 Subject: [PATCH] Add validator --- build.gradle.kts | 1 + .../demo/controllers/UsersController.kt | 12 ++++--- .../kotlin/com/example/demo/entities/User.kt | 14 +++++++-- .../demo/exceptions/GlobalExceptionHandler.kt | 31 +++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/com/example/demo/exceptions/GlobalExceptionHandler.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3bee12f..d889d89 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,6 +23,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-validation") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.xerial:sqlite-jdbc") diff --git a/src/main/kotlin/com/example/demo/controllers/UsersController.kt b/src/main/kotlin/com/example/demo/controllers/UsersController.kt index 027a255..d432068 100644 --- a/src/main/kotlin/com/example/demo/controllers/UsersController.kt +++ b/src/main/kotlin/com/example/demo/controllers/UsersController.kt @@ -2,7 +2,10 @@ package com.example.demo.controllers import com.example.demo.entities.User import com.example.demo.repositories.UserRepository +import jakarta.validation.Valid import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @RestController @@ -21,14 +24,15 @@ class UsersController { } @PostMapping("/users") - fun create(@RequestBody newUser: User): User { - return repo.save(newUser) + fun create(@Valid @RequestBody newUser: User): ResponseEntity { + val user = repo.save(newUser) + return ResponseEntity(user, HttpStatus.CREATED) } @PatchMapping("/users/{id}") - fun update(@PathVariable id: Long, @RequestBody user: User): User { + fun update(@PathVariable id: Long, @Valid @RequestBody user: User): ResponseEntity { user.id = id - return repo.save(user) + return ResponseEntity(repo.save(user), HttpStatus.OK) } @DeleteMapping("/users/{id}") diff --git a/src/main/kotlin/com/example/demo/entities/User.kt b/src/main/kotlin/com/example/demo/entities/User.kt index b551242..740cc07 100644 --- a/src/main/kotlin/com/example/demo/entities/User.kt +++ b/src/main/kotlin/com/example/demo/entities/User.kt @@ -3,8 +3,16 @@ package com.example.demo.entities import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.Id +import jakarta.validation.constraints.Email +import jakarta.validation.constraints.NotEmpty @Entity -class User( - @Id @GeneratedValue var id: Long? = null, - var login: String) \ No newline at end of file +class User { + @Id + @GeneratedValue + var id: Long? = null; + + @NotEmpty(message = "Login can't be empty") + @Email(message = "The email address is invalid.") + lateinit var login: String; +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exceptions/GlobalExceptionHandler.kt b/src/main/kotlin/com/example/demo/exceptions/GlobalExceptionHandler.kt new file mode 100644 index 0000000..02c0fb2 --- /dev/null +++ b/src/main/kotlin/com/example/demo/exceptions/GlobalExceptionHandler.kt @@ -0,0 +1,31 @@ +package com.example.demo.exceptions + +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.HttpStatusCode +import org.springframework.http.ResponseEntity +import org.springframework.validation.FieldError +import org.springframework.web.bind.MethodArgumentNotValidException +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.context.request.WebRequest +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler +import java.util.stream.Collectors + + +@ControllerAdvice +class GlobalExceptionHandler : ResponseEntityExceptionHandler() { + + override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, status: HttpStatusCode, request: WebRequest): ResponseEntity? { + val body: MutableMap> = HashMap() + + val errors = ex.bindingResult + .fieldErrors + .stream() + .map { obj: FieldError -> obj.defaultMessage } + .collect(Collectors.toList()) + + body["errors"] = errors + + return ResponseEntity(body, HttpStatus.BAD_REQUEST) + } +} \ No newline at end of file