V2.1. type 에러 확인

알파로그·2023년 3월 26일
0

Spring MVC 활용 기술

목록 보기
33/42

🔗 요구사항 확인

✏️ Binding Result 의 type 에러 처리

📍 기본 제공기능

  • BindingResult 를 적용하면 Controller 에 별도의 로직을 추가하지 않아도 type 을 잘못 요청할경우 기본적으로 에러메시지를 출력해준다.
    • Entity 나 DTO 에서 type 문제가 발생할경우 에러를 발생시키지 않고 BindingResult 로 에러를 넘기고 Controller 를 정상 작동시킴
    • ⚠️ 매개변수 위치가 다르면 작동이 안될 수 있음

  • 콘솔을 확인해도 필드에 문제가 있다는 메시지를 확인할 수 있음
Field error in object 'item' on field 'price'
    : rejected value []; codes [typeMismatch.item.price,typeMismatch.price,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [item.price,price]; arguments []; default message [price]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'price'; nested exception is java.lang.NumberFormatException: For input string: "ㅂ"]

⚠️ BindingResult 이 없거나, 잘못된 위치에 선언되어있다면 400 에러가 발생한다.

profile
잘못된 내용 PR 환영

0개의 댓글