본문 바로가기

Spring/Spring

[Spring] BindingFailure

반응형

BindingFailure

  • Type - Boolean
  • 역할 - 이 에러가 바인딩 실패인지, 검증 실패인지 구분해준다.

 

	/**
	 * Create a new FieldError instance.
	 * @param objectName the name of the affected object
	 * @param field the affected field of the object
	 * @param rejectedValue the rejected field value
	 * @param bindingFailure whether this error represents a binding failure
	 * (like a type mismatch); else, it is a validation failure
	 * @param codes the codes to be used to resolve this message
	 * @param arguments the array of arguments to be used to resolve this message
	 * @param defaultMessage the default message to be used to resolve this message
	 */
	public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure,
			@Nullable String[] codes, @Nullable Object[] arguments, @Nullable String defaultMessage) {

		super(objectName, codes, arguments, defaultMessage);
		Assert.notNull(field, "Field must not be null");
		this.field = field;
		this.rejectedValue = rejectedValue;
		this.bindingFailure = bindingFailure;
	}

 

그렇다면 제일 궁금한 언제 True이고 언제 False로 해야 하는지 알아보겠습니다.

1. True

bindingFailure는 말 그대로 바인딩을 실패했냐? 라고 묻는 변수라고 볼 수 있습니다. 바인딩이란 웹 요청 파라미터를 자바 객체에 매핑하는 과정으로 int 타입의 price를 받아야 하는데 "abc"라는 문자열이 왔다면 이는 바인딩에 실패하는 것입니다.

이런 경우 스프링이 내부적으로 바인딩 실패로 처리하게 됩니다.

2. False

데이터 바인딩은 되었지만 검증에서 실패한 경우에 쓰인다. 예를 들어서 1~100 사이의 숫자가 들어와야 하는데 1000이 들어오면 이는 바인딩은 되었지만 1~100 사이인가? 라는 검증에 실패했다. 그래서 이 때는 바인딩을 실패했냐? 라는 질문에 실패한 건 아니다 라는 답을 해야 하기 때문에 False를 쓴다.

반응형