-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MATE-112 : [FEAT] Pageable 객체 유효성 검증 커스텀 어노테이션 구현 (#102)
* MATE-112 : [CHORE] 패키지 구조 수정 - utils 패키지 삭제, validator 패키지 위치 변경 - test 패키지 FileValidatorTest 클래스 위치 변경 * MATE-112 : [FEAT] Pageable 객체 유효성 검증 커스텀 어노테이션 구현 * MATE-112 : [REFACTOR] @PageableDefault 어노테이션을 @ValidPageable 어노테이션으로 변경 * MATE-112 : [REFACTOR] 기존 Pageable 유효성 검증 로직 삭제 * MATE-112 : [FEAT] PageResponse 생성 메서드 구현 * MATE-112 : [REFACTOR] 중복 코드 제거
- Loading branch information
Showing
23 changed files
with
176 additions
and
163 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/mate/common/config/WebMvcConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.mate.common.config; | ||
|
||
import com.example.mate.common.validator.ValidPageableArgumentResolver; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
|
||
private final ValidPageableArgumentResolver validPageableArgumentResolver; | ||
|
||
public WebMvcConfig(ValidPageableArgumentResolver validPageableArgumentResolver) { | ||
this.validPageableArgumentResolver = validPageableArgumentResolver; | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(validPageableArgumentResolver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...common/utils/validator/EnumValidator.java → .../mate/common/validator/EnumValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ate/common/utils/validator/ValidEnum.java → ...mple/mate/common/validator/ValidEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/example/mate/common/validator/ValidPageable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.mate.common.validator; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.data.domain.Sort.Direction; | ||
|
||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.PARAMETER}) | ||
public @interface ValidPageable { | ||
|
||
@AliasFor("size") | ||
int value() default 10; | ||
|
||
@AliasFor("value") | ||
int size() default 10; | ||
|
||
int page() default 0; | ||
|
||
String[] sort() default {}; | ||
|
||
Direction direction() default Direction.ASC; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/mate/common/validator/ValidPageableArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.mate.common.validator; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableHandlerMethodArgumentResolver; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
/** | ||
* 커스텀 Pageable 리졸버로, {@code @ValidPageable} 어노테이션이 붙은 Pageable 매개변수의 유효성 검사를 수행합니다. | ||
*/ | ||
@Component | ||
public class ValidPageableArgumentResolver extends PageableHandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public Pageable resolveArgument(MethodParameter methodParameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
WebDataBinderFactory binderFactory) { | ||
|
||
// 기본 Pageable 생성 | ||
Pageable pageable = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory); | ||
|
||
// @ValidPageable 어노테이션 확인 | ||
ValidPageable validPageable = methodParameter.getParameterAnnotation(ValidPageable.class); | ||
|
||
int pageNumber = Math.max(pageable.getPageNumber(), validPageable != null ? validPageable.page() : 0); | ||
int pageSize = pageable.getPageSize() > 0 | ||
? pageable.getPageSize() | ||
: validPageable != null ? validPageable.size() : 10; | ||
|
||
return PageRequest.of(pageNumber, pageSize, pageable.getSort()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/example/mate/domain/goods/dto/request/GoodsPostRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/example/mate/domain/goods/dto/request/GoodsReviewRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.