인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

Byunghyun Yoon님의 프로필 이미지
Byunghyun Yoon

작성한 질문수

스프링 MVC 2편 - 백엔드 웹 개발 활용 기술

@ControllerAdvice

에러 메시지 적용

작성

·

519

0

안녕하세요!

수업 잘 듣고 있습니다.

지금까지 배운 메시지, Bean Validation, Controller Advice를 적용해서 제 프로젝트에 적용하려고 하는데 정상 작동이 안되네요

저는 Bean Vailidation에서 검증에 실패하면 Controller Advice에서 MethodArgumentNotValidateException을 받아 response에 메시지를 errors.properties에 있는 값으로 보내고 싶은데 정상 동작하지 않고 default message로 계속 보내집니다.

일단 설정 파일에 message.basename에 errors 는 추가하고 errors에서 메시지 가져오는 테스트는 정상 작동 하였습니다.

그리고 Controller에 @Validated 를 추가하였고 bindResult는 필요없어서 추가하지 않았습니다.

실제 검증실패 테스트에서 유효성 검사 하는거 확인하였습니다.

문제는 메시지를 errors.properties에서 가져오지 않는것인데..메시지 코드생성도 확인했고, 메시지 코드대로 errors.properties에 작성하였습니다.

에러 메시지는 아래와 같습니다.

org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.hansung.vinyl.account.dto.AccountResponse> com.hansung.vinyl.account.ui.AccountController.join(com.hansung.vinyl.account.dto.AccountRequest): [Field error in object 'accountRequest' on field 'email': rejected value [123]; codes [Email.accountRequest.email,Email.email,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountRequest.email,email]; arguments []; default message [email],[Ljavax.validation.constraints.Pattern$Flag;@692fa174,.*]; default message [올바른 형식의 이메일 주소여야 합니다]] 

메시지 코드가 Email 로 생성되어서 errors.properties에 Email=이메일 형식이 맞지 않습니다. 이렇게 설정되어있구요..ㅜ

혹시 제가 놓친것이 있을까요?

답변 1

2

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. Byunghyun Yoon님

해당 기능을 API로 제공하면서 문제가 발생한 것일까요?

그렇다면 다음을 확인해주세요.

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

감사합니다.

Byunghyun Yoon님의 프로필 이미지
Byunghyun Yoon

작성한 질문수

질문하기