BACKEND/Trouble Shooting

No validator could be found for constraint 'jakarta.validation.constraints.NotEmpty' validating type 'java.lang.Long'.

이-프 2023. 12. 19. 15:56

문제

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: jakarta.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'jakarta.validation.constraints.NotEmpty' validating type 'java.lang.Long'. Check configuration for 'tripId'] with root cause

문제 해석

requestDTO를 생성할 때, NULL 값 유효성 체크를 위해 ‘javax.validation.constraints’ 패키지의 @NotEmpty 어노테이션을 사용했다. 하지만, ‘java.lang.Long’ 타입에서 @NotEmpty를 붙일 수 없으니 필드 ‘tripId’를 체크해보라고 한다.

javax.validation.constraints 의 NULL 관련 Annotation들

https://for-if.tistory.com/58

해결 방안

해결 전 CommentRequestDTO.java

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommentRequestDTO {

    @NotEmpty(message = "여행 ID를 입력하세요.")
    private Long tripId;
    @NotEmpty(message = "회원 ID를 입력하세요.")
    private Long memberId;
    @NotEmpty(message = "댓글을 입력하세요.")
    private String content;
}
  • Long Type인 tripId, memberId 모두 @NotEmpty를 사용중이다.

해결 후 CommentRequestDTO.java

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommentRequestDTO {

    @NotNull(message = "여행 ID를 입력하세요.")
    private Long tripId;
    @NotNull(message = "회원 ID를 입력하세요.")
    private Long memberId;
    @NotEmpty(message = "댓글을 입력하세요.")
    private String content;
}

@NotEmpty 어노테이션을 @NotNull로 수정해주니 잘된다.

🔗 참고 url

https://jerryjerryjerry.tistory.com/139


Uploaded by N2T