• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Querydsl 지원 클래스를 사용할때 sort 익셉션 처리관련 질문

21.09.26 20:41 작성 조회수 230

0

 

QueryDsl 지원 클래스를 직접 사용할 경우, paging, sorting의 로직을 한곳에서 관리할 수 있는데

 

@GetMapping("/search")
public ResponseEntity<ResponseOKDto<GetPagePostsResponseDto>> getPagePostsSearch(
PostSearchCondition condition,
Pageable pageable, @ApiIgnore HttpSession session){

Account account = accountService.checkSessionAndFindAccountWithActivityArea(session);
Page<Post> pagePost = checkStatusParamAndFindPosts(account, condition, pageable);

return new ResponseEntity<>(new ResponseOKDto<>(new GetPagePostsResponseDto(pagePost)), HttpStatus.OK);
}

이런식으로 만들었을 경우, 프론트에서 해당 도메인 클래스에 없는 프로퍼티에 대해 잘못 sorting을 요청했을 때 익셉션 처리를 해주려고하는데요

 

querydsl 지원 클래스내에서 해당 익셉션을 처리할 경우,

아래와 같이 다른 도메인 controller에서도 따로 익셉션을 처리하지 않아도 된다는 장점을 가지는 것 같은데요

 

protected <T> Page<T> applyPagination(Pageable pageable, Function<JPAQueryFactory, JPQLQuery> contentQuery,
Function<JPAQueryFactory, JPQLQuery> countQuery){
checkSortProperties(pageable);

JPQLQuery jpaContentQuery = contentQuery.apply(queryFactory);
JPQLQuery jpaCountQuery = countQuery.apply(queryFactory);
List<T> content = getQuerydsl().applyPagination(pageable, jpaContentQuery).fetch();
return PageableExecutionUtils.getPage(content, pageable, jpaCountQuery::fetchCount);
}

private void checkSortProperties(Pageable pageable) {
List<String> domainFields = Arrays.stream(domainClass.getDeclaredFields()).map(Field::getName).collect(toList());
List<String> sortFields = pageable.getSort().get().map(Sort.Order::getProperty).collect(toList());
if (!domainFields.containsAll(sortFields)) {
throw new IllegalSortArgumentException("invalid sort property, properties must be in " + domainFields);
}
}

 

고민이 되는 점은 컨트롤러에서 미리 걸러줘야 할 것 같은데,

레포지토리내에서 익셉션 처리를 해줘도 되나?라는 의문이 들었습니다.

 

익셉션 처리 위치를 이렇게 해줘도 될까요?

아니면 번거롭더라도 각각의 도메인에 대해 일일히 컨트롤러 클래스 내에서 처리를 해줘야 할까요?

 

답변주시면 감사드리겠습니다.

 

 

답변 1

답변을 작성해보세요.

1

안녕하세요. 승현님

다음을 참고해주세요.

https://www.inflearn.com/questions/277568

감사합니다.