Backend/Spring Boot
원본 글 보기

[Spring Boot] 댓글이 없는 게시글이 조회되지 않는 문제 해결

[ 상황 ]

댓글 기능이 있는 게시글을 구현했는데,

게시글에 댓글이 없으면 게시글 조회가 안됐다.

그림과 같은 상황

[ 해결 ]

게시글 조회 시 댓글이 없어도 에러가 안나게,

게시글 조회 service에 if로 예외처리를 해줬다.



public 게시글DTO readService(int idx) {

Set<RcommentDTO> rcommentDTOSet = new HashSet<>();
        result.forEach(arr->{
            Rcomment rcomment = (Rcomment) arr[3];
if (rcomment!= null) {
                RcommentDTO rcommentDTO = RcommentDTO.builder()
.idx(rcomment.getIdx())
.writer(rcomment.getWriter())
.contents(rcomment.getContents())
.build();
                rcommentDTOSet.add(rcommentDTO);
}
});
}

해결!