Add validator

This commit is contained in:
Florian RICHER 2024-01-02 22:12:49 +01:00
parent 30713ca8d5
commit 640d1313f9
4 changed files with 51 additions and 7 deletions

View file

@ -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")

View file

@ -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<User> {
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> {
user.id = id
return repo.save(user)
return ResponseEntity(repo.save(user), HttpStatus.OK)
}
@DeleteMapping("/users/{id}")

View file

@ -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)
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;
}

View file

@ -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<Any>? {
val body: MutableMap<String, List<String?>> = HashMap()
val errors = ex.bindingResult
.fieldErrors
.stream()
.map { obj: FieldError -> obj.defaultMessage }
.collect(Collectors.toList())
body["errors"] = errors
return ResponseEntity(body, HttpStatus.BAD_REQUEST)
}
}