Skip to content

Conversation

@Kama-Pushka
Copy link
Owner

Студент: Аптуликсанов Руслан Германович.
Преподаватель: Никита Иванов

  • Сделать свою аннотацию и кастомный валидатор к ней и красивый ответ в случае возникновения ошибок (RussianFullName, RussianFullNameConstraintValidator и ArticleExceptionHandler)
  • Аннотацию для валидации, объединяющую несколько других аннотаций (Title)

изображение
изображение

implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.projectlombok:lombok:1.18.30")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compileOnly

@Service
@Validated
public class ArticleService {
public String create(@Valid ArticleIn article) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще такой подход имеет место (использовать аннотации валидации на уровне сервиса) и как правило применяется не к самому сервису, а к его интерфейсу. Но все же, на будущее, следует валидации выполнять на уровне контроллера. Как правило во всех проектах у тебя должны быть обработчики и на MethodArgumentNotValidException, и на ConstraintViolationException


@PostMapping("/create")
@ResponseStatus(HttpStatus.CREATED)
public String register(@RequestBody ArticleIn article) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возвращать следует ДТО

import org.javaspringcourse.validation.Title;

@Data
public class ArticleIn {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Начиная с java 17 для дто следует использовать record, когда это возможно. Или хотя бы использовать @Value из lombok вместо @Data, т.к. дто должны быть неизменяемыми

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants