MVC 기반(Model 2)의 Web Application을 작성하기 위한 Spring Framework의 하위 모듈

Handler의 한 종류로 MVC에서 Client의 요청을 받아들이는 역할을 하는 클래스
요청 처리 메서드를 작성하기 위한 annotation
value 속성을 이용해서 처리할 경로 지정
method 속성으로 Get/POST 등 매핑
축약형 예시
@PostMapping
@GetMapping
🔁 Forward (기본)
@RequestMapping("/")
public String welcome(Model model){
model.addAttribute("message", service.sayHello());
return "index";
}
➡️ Redirect
@RequestMapping("/redir")
public String redir(Model model){
return "redirect:/";
}
📤 JSON 응답 (@ResponseBody)
@GetMapping("/json")
public @ResponseBody Map<String, Object> json(){
return Map.of("name", "hong gil dong", "age", 10);
}
MockHttpServletRequestBuilder builder = get("/add")
.param("a", "4.5").param("b", "3")
.cookie(new Cookie("name", "홍길동"))
.sessionAttr("loginUser", member);
mock.perform(builder);
actions.andExpect(handler().handlerType(SimpleController.class))
.andExpect(forwardedUrl("/WEB-INF/views/mvc/simple.jsp"))
.andExpect(view().name("mvc/simple"))
.andExpect(model().attribute("data", "Hello"))
.andExpect(status().is(200));
mock.perform(builder).andExpect(status().is(200))
.andDo(print());
actions.andExpect(handler().methodName("json"))
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is(200))
.andExpect(jsonPath("$.name", equalTo("hong gil dong")))
.andExpect(jsonPath("$.age", equalTo(10)));
다양한 타입의 파라미터를 순서 무관하게 전달 가능
예시 타입: HttpServletRequest, HttpServletResponse, Model, HttpSession, Locale 등
기존 방식: redirect를 위해 session 사용 → 불편함 😖
✅ 해결: RedirectAttributes 와 Flash Scope
request < flash < session