요청 : GET 요청
@Controller
public ModelAndView method()
{
return new ModelAndView("view-name");
}
@Controller
public String method()
{
return "view-name";
}
요청 : 쿼리 파라미터 GET 요청, <form> POST 요청
@Controller
public ModelAndView method(HttpServletRequest request, HttpServletResponse response)
{
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(age);
ModelAndView mv = new ModelAndView("view-name");
mv.addObject("member", member);
return "view-name";
}
@Controller
public String method(@RequestParam("age") int age, Model model)
{
Member member = new Member(age);
model.addAttribute("member", member);
return "view-name";
}
@Controller
public String method(@ModelAttribute Member member, Model model)
{
model.addAttribute("member", member);
return "view-name";
}
@Controller
public String method(@ModelAttribute("member") Member member, Model model)
{
model.addAttribute("member", member);
return "view-name";
}
@Controller
public String method(@ModelAttribute("member") Member member, Model model)
{
model.addAttribute("member", member);
return "view-name";
}
요청 : HTTP Message Body에 단순 텍스트 POST 요청
※ 쿼리 파라미터 GET 요청, <form> POST 요청과 다르게 HTTP Message Body 요청은 @RequestParam, @ModelAttribute 사용 불가
@Controller
public void method(HttpServletRequest request, HttpServletResponse response)
{
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF-8);
response.getWriter().write("ok");
}
@Controller
public void method(InputStream inputStream, Wirter responseWriter)
{
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF-8);
responseWriter.write("ok");
}
@Controller
public HttpEntity<String> method(HttpEntity<String> httpEntity)
{
String messageBody = httpEntity.getBody();
return new HttpEntity<>("ok");
}
@Controller
public HttpEntity<String> method(HttpEntity<String> httpEntity)
{
String messageBody = httpEntity.getBody();
return new ResponseEntity<>("ok", HttpStatus.OK);
}
@Controller
@ReponseBody
public String method(@RequestBody String messageBody)
{
return "ok";
}
요청 : HTTP Message Body에 JSON POST 요청
@Controller
public void method(HttpServletRequest request, HttpServletResponse response)
{
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF-8);
Member member = objectMapper.readValue(messageBody, Member.class());
response.getWriter().write("ok");
}
@Controller
@ResponseBody
public void method(@RequestBody String messageBody)
{
Member member = objectMapper.readValue(messageBody, Member.class());
return "ok";
}
@Controller
@ResponseBody
public String method(HttpEntity<Member> member)
{
Member member = httpEntity.getBody();
return "ok";
}
@Controller
@ResponseBody
public void method(@RequestBody Member member)
{
return "ok";
}
@Controller
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Member method(@RequestBody Member member)
{
return member;
}
@RequestBody 요청
- JSON 요청 -> HTTP 메세지 컨버터 -> 객체
@RequestBody 요청
- 객체 -> HTTP 메세지 컨버터 -> JSON 응답