TIL

김나영·2023년 8월 18일
0

TIL

목록 보기
43/43

2023.08.15

✅ Fact

  • 실전 프로젝트

트러블 슈팅

  • 메인 페이지에서 카테고리 별 게시글 총개수 반환하는 코드 추가

  • 포스트맨 테스트 시 403에러 발생

    • The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference.(요청은 유효한 요청이었지만, 서버가 이에 응답을 거부하고 있습니다. 401 Unauthorized 응답과는 달리, 인증을 해도 상관이 없습니다.)

1. url 문제인 줄 알고 api 수정

ex) api/homepostcount --> api/home/postcount --> api/count

  • 403 에러 발생

2. 코드 문제인가? 해서 코드 수정

    public HomeCountryCountDto getCountryCount() {

        QPost post = QPost.post;


        Long ASIAPostCount = jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(Category.PostCategory.ASIA))
                .fetchOne();


        Long AFRICAPostCount = jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(Category.PostCategory.AFRICA))
                .fetchOne();


        Long EUROPEPostCount = jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(Category.PostCategory.EUROPE))
                .fetchOne();

        Long OCEANIAPostCount = jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(Category.PostCategory.OCEANIA))
                .fetchOne();

        Long AMERICAPostCount = jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(Category.PostCategory.AMERICA))
                .fetchOne();

        return new HomeCountryCountDto(ASIAPostCount, AFRICAPostCount, EUROPEPostCount, OCEANIAPostCount, AMERICAPostCount);
    }
  • 처음 시도한 코드
        Map<String, Long> countryCounts = new LinkedHashMap<>();

        Long asiaPostCount = getPostCountByCategory(Category.PostCategory.ASIA);
        Long africaPostCount = getPostCountByCategory(Category.PostCategory.AFRICA);
        Long europePostCount = getPostCountByCategory(Category.PostCategory.EUROPE);
        Long oceaniaPostCount = getPostCountByCategory(Category.PostCategory.OCEANIA);
        Long americaPostCount = getPostCountByCategory(Category.PostCategory.AMERICA);

        countryCounts.put("asiaPostCount", asiaPostCount);
        countryCounts.put("africaPostCount", africaPostCount);
        countryCounts.put("europePostCount", europePostCount);
        countryCounts.put("oceaniaPostCount", oceaniaPostCount);
        countryCounts.put("americaPostCount", americaPostCount);

        return countryCounts;
    }

    private Long getPostCountByCategory(Category.PostCategory category) {
        return jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(category))
                .fetchOne();
    }
}
  • 두 번째 시도한 코드
        Long asiaPostCount = getPostCountByCategory(Category.PostCategory.ASIA);
        Long africaPostCount = getPostCountByCategory(Category.PostCategory.AFRICA);
        Long europePostCount = getPostCountByCategory(Category.PostCategory.EUROPE);
        Long oceaniaPostCount = getPostCountByCategory(Category.PostCategory.OCEANIA);
        Long americaPostCount = getPostCountByCategory(Category.PostCategory.AMERICA);

        List<HomeCountryCountDto> countryCounts = new ArrayList<>();
        countryCounts.add(new HomeCountryCountDto(
                Category.PostCategory.ASIA.getValue(),
                asiaPostCount,
                africaPostCount,
                europePostCount,
                oceaniaPostCount,
                americaPostCount
        ));

        countryCounts.add(new HomeCountryCountDto(
                Category.PostCategory.AFRICA.getValue(),
                asiaPostCount,
                africaPostCount,
                europePostCount,
                oceaniaPostCount,
                americaPostCount
        ));

        countryCounts.add(new HomeCountryCountDto(
                Category.PostCategory.EUROPE.getValue(),
                asiaPostCount,
                africaPostCount,
                europePostCount,
                oceaniaPostCount,
                americaPostCount
        ));

        countryCounts.add(new HomeCountryCountDto(
                Category.PostCategory.OCEANIA.getValue(),
                asiaPostCount,
                africaPostCount,
                europePostCount,
                oceaniaPostCount,
                americaPostCount
        ));

        countryCounts.add(new HomeCountryCountDto(
                Category.PostCategory.AMERICA.getValue(),
                asiaPostCount,
                africaPostCount,
                europePostCount,
                oceaniaPostCount,
                americaPostCount
        ));

        return countryCounts;
    }

    private Long getPostCountByCategory(Category.PostCategory category) {
        return jpaQueryFactory
                .select(post.id.count())
                .from(post)
                .where(post.category.eq(category))
                .fetchOne();
    }
  • 세 번째 시도한 코드

3. 혹시..? 하고 WebSecurityConfig에서 api 요청 추가

  • .requestMatchers(HttpMethod.GET, "/api/count").permitAll()

4. 포스트맨으로 테스트 시 200 OK 각 대륙별 게시글 총개수 반환 성공


  • 에러 코드 확인 잘 하자!!!

    • 403 오류는 클라이언트가 서버에 올바른 요청을 보냈으나, 서버가 이 요청에 응답할 권한이 없거나 거부되었음을 나타내므로 보안 관련 이슈나 권한 설정, 보내는 url 위추로 꼼꼼하게 확인하자!!

0개의 댓글