Failed to convert property value of type java.lang.String to required type java.lang.Integer

By | 2023년 2월 27일
Table of Contents

Failed to convert property value of type java.lang.String to required type java.lang.Integer

@Valid 유효성 검사시 Integer 숫자형에 숫자 이외 문자가 입력된 경우,
위와 같은 오류가 발생한다.

ControllerAdvice

서버 사이드에서는 최소한의 오류 메시지만 표시하고,
클라이언트 사이드에서 타입체킹 하는 것이 가장 낫다고 생각된다.

@ControllerAdvice
public class ControllerAdviceHandler {
    @ExceptionHandler({BindException.class})
    protected ResponseEntity<?> handleBindException(HttpServletResponse response, BindException ex) throws IOException {
        String message = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        assert message != null;
        if (message.contains("NumberFormatException")) {
            message = String.format("허용되지 않는 값 %S 이 입력되었습니다.", ex.getBindingResult().getFieldErrors().get(0).getRejectedValue());
        }
        ScriptUtil.alertAndBackPage(response, message);
        return null;
    }
}

JSP

        <tr>
            <td><label for="sizeWidth">가로</label></td>
            <td>
                <input id="sizeWidth" name="sizeWidth" type="number" value="${item.sizeWidth}" />
            </td>
        </tr>

One thought on “Failed to convert property value of type java.lang.String to required type java.lang.Integer

답글 남기기