From ca21901618e6763608ffbba5da778fffc7b2a3fd Mon Sep 17 00:00:00 2001 From: uPagge Date: Mon, 22 Mar 2021 17:38:27 +0300 Subject: [PATCH] valid requests parameters --- .../controller/PersonController.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java b/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java index cd72e00..9fbb042 100644 --- a/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java +++ b/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java @@ -4,21 +4,44 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.sadtech.example.springvalidation.dto.PersonDto; import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +@Validated @RestController @RequestMapping("/api/person") -@Tag(name = "Пользователи системы") +@Tag(name = "Пользователи системы", description = "Валидация на уровне контроллера") public class PersonController { @PostMapping - @Operation(summary = "Валидация на уровне контроллера") - public ResponseEntity valid(@Valid @RequestBody PersonDto personDto) { + @Operation(summary = "Сохранение пользователя") + public ResponseEntity save(@Valid @RequestBody PersonDto personDto) { + return ResponseEntity.ok("valid"); + } + + @GetMapping("{id}") + @Operation(summary = "Получить пользователя по id") + public ResponseEntity getById( + @PathVariable("id") @Min(0) int personId + ) { + return ResponseEntity.ok("valid"); + } + + @GetMapping + @Operation(summary = "Получить пользователя по имени") + public ResponseEntity getByName( + @RequestParam("name") @NotBlank String name + ) { return ResponseEntity.ok("valid"); }