HTTP - 요청 파라미터 @ModelAttribute
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@RequestParam String username,
@RequestParam int age){
HelloData helloData = new HelloData();
helloData.setUsername(username);
helloData.setAge(age);
log.info("username={}, age={}", helloData.getUsername(),
helloData.getAge());
return "ok";
}
- 놀랍게도 위 코드랑 아래 코드랑 같은 코드이다.
- 아래 코드는 @ModelAttribute를 사용해 helloData의 객체가 생성되고, 요청 파라미터의 값을 모두 바인딩(입력) 해준다.
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData){
log.info("username={}, age={}", helloData.getUsername(),
helloData.getAge());
return "ok";
}
- 정리
- @RequestParam도 생략가능, @ModelAttribute도 생략이 가능함으로 명시해줘서 혼란을 방지하는것이 좋다.