오류해결

Spring에서 계속 null값이 들어오는 에러 발생

indeep 2023. 5. 8. 22:11
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="./include/header.jsp"%>
	<%@ include file="./include/bootstrap.jsp"%>

	<form action="${pageContext.request.contextPath}/regist" method="post" enctype="multipart/form-data">
		<div>
			<label for="pCode">상품코드</label>
			<input type="text" name="pCode">
		</div>
		<div>
			<label for="pName">상품이름</label>
			<input type="text" name="pName">
		</div>
		<div>
			<label for="price">상품가격</label>
			<input type="number" name="price">
		</div>
		<div>
			<label for="quantity">수량</label>
			<input type="number" name="quantity">
		</div>
		<div>
			<label for="brand">브랜드</label>
			<input type="text" name="brand">
		</div>
		<div>
			<label for="pDesc">설명</label>
			<input type="text" name="pDesc">
		</div>
		
		<input type="submit" value="입력" class="btn btn-primary"><input type="reset" value="취소" class="btn btn-primary">
	</form>

</body>
</html>

위 코드에서 price, quantity만 int값이고 나머지는 String으로 설정돼 있었다.

코드를 실행해서 전부 숫자를 넣어서 입력했더니.

 

400 에러 발생과 함께 price와 quantity의 타입이 맞지 않는다는 에러가 발생했다.

 

WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExcep tionResolver - Resolved [org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 2 errors<EOL>Field error in object 'product' on field 'price': rejected value [null]; codes [typeMismatch.product.price,typeMismatch.price,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.price,price]; arguments []; default message [price]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]<EOL>Field error in object 'product' on field 'quantity': rejected value [null]; codes [typeMismatch.product.quantity,typeMismatch.quantity,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.quantity,quantity]; arguments []; default message [quantity]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]]

 

다음과 같은 에러가 발생했는데, 인터넷에 찾아보니 int에 null값이 들어오면 처리하지 못한다는 내용이다.

그래서 전부 String타입으로 바꿔서 진행을 하고 컨트롤러단에서 출력을 해봤더니 pCode부터 null값이 계속 넘겨지는 것을 확인했다.

 

근데 처음부터 int값을 주는데 null이 들어온다는 게 이상함을 느꼈고 2시간 삽질을 했다.

 

해결 방법

결과는 enctype="multipart/form-data" 에서 발생했던 문제였다.

enctype은 파일 업로드를 포함한 폼 데이터를 전송하기 위해 사용되지만, 스프링에서 파일 업로드를 처리하기 위해서는 추가 구성이 필요했다. 그렇지 않으면, 폼의 일반 텍스트 필드값들이 제대로 바인딩되지 않아서 계속 null이 들어왔던 문제였다.

우선 임시방편으로 파일을 그냥 제거해 버렸더니 잘 입력을 받더라…

 

반응형