공통점
둘 다 dto의 벨리데이션을 컨트롤러에서 파라미터를 받을때 검증하는데 사용할 수 있다.
차이점
@Validated는 그룹별로(get, post,,,) 별로 벨리데이션을 할수있어서 그룹별로 더욱 섬세하게 검증할 수 있다.
사용방법
1. DTO에서 그룹을 지정한다.
@Getter
@Setter
public class AccountLevelManagementCommand {
@NotBlank(
message = "Account ID cannot be empty.",
groups = {AccountLevelManagementRequestValidationGroup.class,
AccountLevelManagementSubmitValidationGroup.class})
private String id;
@NotNull(
message = "Level cannot be null.",
groups = {AccountLevelManagementSubmitValidationGroup.class})
private ManagerLevel levelName;
@NotNull(
message = "Operation cannot be null.",
groups = {AccountLevelManagementSubmitValidationGroup.class})
private AccountLevelManagementOperation operation;
}
2. 컨트롤러에서 get, post에 따라 검증할 그룹을 지정할 수 있다.
@Getter
@Setter
public class AccountLevelManagementCommand {
@NotBlank(
message = "Account ID cannot be empty.",
groups = {AccountLevelManagementRequestValidationGroup.class,
AccountLevelManagementSubmitValidationGroup.class})
private String id;
@NotNull(
message = "Level cannot be null.",
groups = {AccountLevelManagementSubmitValidationGroup.class})
private ManagerLevel levelName;
@NotNull(
message = "Operation cannot be null.",
groups = {AccountLevelManagementSubmitValidationGroup.class})
private AccountLevelManagementOperation operation;
}'Java, Spring' 카테고리의 다른 글
| [Spring Boot] @Component 와 @Bean의 차이 (0) | 2021.01.21 |
|---|---|
| [Spring Boot] IoC(Inversion Of Control)란 무엇인가? : 스프링 부트에서의 IoC의 역할 (0) | 2021.01.21 |
| [Spring Boot] Dependency Injection : DI의 개념과 스프링이 DI를 지원하는 방법 (0) | 2021.01.21 |
| [Spring Boot] 독립실행형(standalone) 애플리케이션 : 스프링 컨테이너의 역할 (0) | 2021.01.21 |
| [Spring Boot] @Scheduled 시간 (5분간격, 매시간 1분마다, 자정 1분마다) (0) | 2021.01.21 |