Path Variable 방식
: 서버에 보내려는 데이터를 URL 경로에 추가할 수 있음
🌐 GET http://localhost:8080/hello/request/star/Robbie/age/95// [Request sample] // GET http://localhost:8080/hello/request/star/Robbie/age/95 @GetMapping("/star/{name}/age/{age}") @ResponseBody public String helloRequestPath(@PathVariable String name, @PathVariable int age) { return String.format("Hello, @PathVariable.<br> name = %s, age = %d", name, age); }
Request Param 방식
: 서버에 보내려는 데이터를 URL 경로 마지막에?와&를 사용하여 추가할 수 있음
🌐 GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95// [Request sample] // GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95 @GetMapping("/form/param") @ResponseBody public String helloGetRequestParam(@RequestParam String name, @RequestParam int age) { return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age); }form 태그 POST
: HTML의 form 태그를 사용하여 POST 방식으로 HTTP 요청을 보낼 수 있음
🌐 POST http://localhost:8080/hello/request/form/param
- 이때 해당 데이터는 HTTP Body에
name=Robbie&age=95형태로 담겨져 서버로 전달됨<form method="POST" action="/hello/request/form/model"> <div> 이름: <input name="name" type="text"> </div> <div> 나이: <input name="age" type="text"> </div> <button>전송</button> </form>// [Request sample] // POST http://localhost:8080/hello/request/form/param // Header // Content type: application/x-www-form-urlencoded // Body // name=Robbie&age=95 @PostMapping("/form/param") @ResponseBody public String helloPostRequestParam(@RequestParam String name, @RequestParam int age) { return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age); }
@RequestParam은 생략 가능@RequestParam(required = false)required = false 설정이 안되어있다면, 오류 발생)
@ModelAttribute
- form 태그 POST
🌐 POST http://localhost:8080/hello/request/form/model// [Request sample] // POST http://localhost:8080/hello/request/form/model // Header // Content type: application/x-www-form-urlencoded // Body // name=Robbie&age=95 @PostMapping("/form/model") @ResponseBody public String helloRequestBodyForm(@ModelAttribute Star star) { return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age); }- HTML의 form 태그를 사용하여 POST 방식으로 HTTP 요청을 보낼 수 있음 -> 이때 해당 데이터는 HTTP Body에 `name=Robbie&age=95` 형태로 담겨져서 서버로 전달됨 - `@ModelAttribute` 어노테이션을 사용한 후 Body 데이터를 `Star star` 받아올 객체로 선언함
- Query String 방식
🌐 GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95// [Request sample] // GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95 @GetMapping("/form/param/model") @ResponseBody public String helloRequestParam(@ModelAttribute Star star) { return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age); }- `?name=Robbie&age=95`처럼 데이터가 두 개만 있다면 괜찮지만, 여러 개 있다면 `@RequestParam` 어노테이션으로 하나씩 받아오기 힘들 수 있음 -> 이때 `@ModelAttribute`를 사용하면 Java의 객체로 데이터를 받아올 수 있음 - 파라미터에 선언한 `Star` 객체가 생성되고, 오버로딩된 생성자 혹은 Setter 메서드를 통해 요청된 `name & age`의 값이 담겨짐
@ModelAttribute는 생략 가능하다.@ModelAttribute와 @RequestParam이 모두 생략 가능한데 Spring에서 어떻게 구별할까@RequestParam으로 간주하고, 아니라면 @ModelAttribute가 생략되어있다고 판단한다.
@RequestBody
- HTTP Body에 JSON 데이터를 담아 서버에 전달할 때 해당 Body 데이터를 Java의 객체로 전달 받을 수 있음
- Body JSON 데이터
🌐 POST http://localhost:8080/hello/request/form/json// [Request sample] // POST http://localhost:8080/hello/request/form/json // Header // Content type: application/json // Body // {"name":"Robbie","age":"95"} @PostMapping("/form/json") @ResponseBody public String helloPostRequestJson(@RequestBody Star star) { return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age); }
- HTTP Body에
{"name":"Robbie","age";"95"}JSON형태로 데이터가 서버에 전달되었을 때@RequestBody어노테이션을 사용해 데이터를 객체 형태로 받을 수 있음