[Spring MVC] 들어오는 모든 파라미터에 trim 을 걸어주자

식빵·2023년 11월 23일
1

Spring Lab

목록 보기
25/33

참고:

간단한 건데 계속 까먹어서 기록한다.

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

@Controller
@RequestMapping(value = "/my/controller")
public class MyController {

	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
		dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
	}

	// ... 생략 ...
}

참고로 모든 Controller 에 적용하고 싶으면 Controller Advice 를 통해서 작업이 가능하다.

profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글