valid body controller

This commit is contained in:
uPagge 2021-03-22 17:24:19 +03:00
parent 04112db62c
commit a6c000c1cf
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
3 changed files with 63 additions and 5 deletions

16
pom.xml
View File

@ -20,13 +20,19 @@
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.0.Final</version>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>

View File

@ -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<String> valid(@Valid @RequestBody PersonDto personDto) {
return ResponseEntity.ok("valid");
}
}

View File

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