From a6c000c1cfac282cde01618538a4d79a1005c439 Mon Sep 17 00:00:00 2001 From: uPagge Date: Mon, 22 Mar 2021 17:24:19 +0300 Subject: [PATCH] valid body controller --- pom.xml | 16 +++++++---- .../controller/PersonController.java | 25 +++++++++++++++++ .../springvalidation/dto/PersonDto.java | 27 +++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java create mode 100644 src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java diff --git a/pom.xml b/pom.xml index 62b4362..b8b565c 100644 --- a/pom.xml +++ b/pom.xml @@ -20,13 +20,19 @@ - javax.validation - validation-api + org.springframework.boot + spring-boot-starter-validation + + + + io.swagger.core.v3 + swagger-annotations + 2.1.6 - org.hibernate - hibernate-validator - 7.0.0.Final + org.springdoc + springdoc-openapi-ui + 1.5.2 diff --git a/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java b/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java new file mode 100644 index 0000000..cd72e00 --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/controller/PersonController.java @@ -0,0 +1,25 @@ +package org.sadtech.example.springvalidation.controller; + +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.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; + +@RestController +@RequestMapping("/api/person") +@Tag(name = "Пользователи системы") +public class PersonController { + + @PostMapping + @Operation(summary = "Валидация на уровне контроллера") + public ResponseEntity valid(@Valid @RequestBody PersonDto personDto) { + return ResponseEntity.ok("valid"); + } + +} diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java new file mode 100644 index 0000000..4e908ec --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java @@ -0,0 +1,27 @@ +package org.sadtech.example.springvalidation.dto; + +import lombok.Getter; +import lombok.Setter; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; + +@Getter +@Setter +public class PersonDto { + + private Long id; + + @NotBlank + private String name; + + @Min(1) + @Max(10) + private int numberBetweenOneAndTen; + + @Pattern(regexp = "^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$") + private String ipAddress; + +}