Обновление зависимостей

This commit is contained in:
Struchkov Mark 2024-11-04 11:55:44 +03:00
parent 1309a553a4
commit 95a6621d6c
No known key found for this signature in database
GPG Key ID: A3F0AC3F0FA52F3C
12 changed files with 132 additions and 33 deletions

15
pom.xml
View File

@ -2,10 +2,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
@ -15,7 +16,7 @@
<name>spring-validation</name>
<properties>
<java.version>17</java.version>
<java.version>21</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -28,15 +29,11 @@
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>

View File

@ -1,5 +1,6 @@
package org.sadtech.example.springvalidation.controller;
import jakarta.validation.ConstraintViolationException;
import org.sadtech.example.springvalidation.dto.ValidationErrorResponse;
import org.sadtech.example.springvalidation.dto.Violation;
import org.springframework.http.HttpStatus;
@ -9,7 +10,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

View File

@ -2,7 +2,12 @@ package org.sadtech.example.springvalidation.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.service.PersonService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
@ -13,16 +18,18 @@ 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;
import java.time.LocalDate;
import java.util.List;
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/person")
@Tag(name = "Пользователи системы", description = "Валидация на уровне контроллера")
public class PersonController {
private final PersonService personService;
@PostMapping
@Operation(summary = "Сохранение пользователя")
public ResponseEntity<String> save(@Valid @RequestBody PersonDto personDto) {
@ -45,4 +52,15 @@ public class PersonController {
return ResponseEntity.ok("valid");
}
@GetMapping("birthday")
@Operation(summary = "Получить пользователей в диапазоне ДР")
public ResponseEntity<List<PersonDto>> getByPersonBirthday(
@RequestParam("from") LocalDate from,
@RequestParam("to") LocalDate to
) {
return ResponseEntity.ok(
personService.getAllByHappyBirthdayBetween(from, to)
);
}
}

View File

@ -1,6 +1,7 @@
package org.sadtech.example.springvalidation.controller;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.valid.Marker;
import org.springframework.http.ResponseEntity;
@ -11,7 +12,6 @@ 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;
@Validated
@RestController

View File

@ -12,9 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/no-valid/person")
@Tag(name = "Пользователи системы 2", description = "Валидация на уровне сервиса")
@RequiredArgsConstructor
public class PersonControllerNoValidation {
private final PersonService personService;

View File

@ -0,0 +1,18 @@
package org.sadtech.example.springvalidation.dto;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Address {
@NotBlank
private String state;
@NotBlank
private String city;
}

View File

@ -1,23 +1,23 @@
package org.sadtech.example.springvalidation.dto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;
import org.sadtech.example.springvalidation.valid.CapitalLetter;
import org.sadtech.example.springvalidation.valid.Marker;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
@Entity
@Getter
@Setter
@ -46,4 +46,7 @@ public class PersonDto {
)
private String ipAddress;
@Transient
private Address address;
}

View File

@ -1,10 +1,14 @@
package org.sadtech.example.springvalidation.service;
import jakarta.validation.Valid;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.valid.LocalDateBetween;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
@Service
@Validated
@ -14,4 +18,10 @@ public class PersonService {
// do something
}
@LocalDateBetween
public List<PersonDto> getAllByHappyBirthdayBetween(LocalDate from, LocalDate to) {
System.out.println("valid");
return Collections.emptyList();
}
}

View File

@ -1,7 +1,8 @@
package org.sadtech.example.springvalidation.valid;
import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

View File

@ -1,7 +1,8 @@
package org.sadtech.example.springvalidation.valid;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class CapitalLetterValidator implements ConstraintValidator<CapitalLetter, String> {

View File

@ -0,0 +1,23 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Target(METHOD)
@Retention(RUNTIME)
public @interface LocalDateBetween {
String message() default "Дата невалидна";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -0,0 +1,28 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraintvalidation.SupportedValidationTarget;
import jakarta.validation.constraintvalidation.ValidationTarget;
import java.time.LocalDate;
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class LocalDateBetweenValidator implements ConstraintValidator<LocalDateBetween, Object[]> {
@Override
public boolean isValid(Object[] value, ConstraintValidatorContext context) {
final Object objDateFrom = value[0];
final Object objDateTo = value[1];
if (
!(objDateFrom instanceof final LocalDate dateFrom)
|| !(objDateTo instanceof final LocalDate dateTo)
) {
throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate.");
}
return dateFrom.isAfter(LocalDate.now()) && dateFrom.isBefore(dateTo);
}
}