์คํ๋ง ํ๋ ์์ํฌ์์ HTTP ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ปจํธ๋กค๋ฌ ๋ฉ์๋๋ก ๋งคํํ๋ ๋ฐฉ๋ฒ์ ๋ค์ํ๋ค. ๊ฐ๊ฐ์ ์ ๋ ธํ ์ด์ ์ ๋ค๋ฅธ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค. ๋ค์์ @RequestBody, @RequestParam, @PathVariable, @ModelAttribute์ ์ฐจ์ด์ ์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ๋ด๋ถ์ ์๋ ๋ฐฉ์์ ๋ํด์ ๋ค๋ค๋ณด์๋ค.
@RequestBody
์ค๋ช
- @RequestBody๋ HTTP ์์ฒญ ๋ณธ๋ฌธ์ ๊ฐ์ฒด์ ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- ์ฃผ๋ก JSON, XML ๋ฑ์ ํฌ๋งท์ผ๋ก ์ ์ก๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํํ ๋ ์ฌ์ฉ๋๋ค.
- JSON ๋ฐ์ดํฐ ๊ฒฝ์ฐ HttpMessageConverter ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด MappingJackson2HttpMessageConverter ํด๋์ค๋ฅผ ํตํด ์์ฒญ ๋ณธ๋ฌธ์ ์ฝ๊ณ , ํด๋น ๊ฐ์ฒด๋ก ๋ณํํ๋ค. MappingJackson2HttpMessageConverter ํด๋์ค์์์ ObjectMapper๋ฅผ ํ์ฉํ๋ค.
์ฌ์ฉ ์
์ปค์คํ ์ปจํธ๋กค๋ฌ
import org.springframework.web.bind.annotation.*;
@RestController
public class MyController {
@PostMapping("/users")
public String createUser(@RequestBody User user) {
// ์์ฒญ ๋ณธ๋ฌธ(JSON)์ User ๊ฐ์ฒด๋ก ๋งคํ
return "User is created with name: " + user.getName();
}
}
User ํด๋์ค
public class User {
private String name;
private String email;
// getters and setters
}
์์ฒญ ์์ (JSON)
{
"name": "John Doe",
"email": "john.doe@example.com"
}
@RequestParam
์ค๋ช
- @RequestParam์ HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ์ ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- ์ฃผ๋ก ์ฟผ๋ฆฌ ์คํธ๋ง ํ๋ผ๋ฏธํฐ๋ ํผ ๋ฐ์ดํฐ์ ๋จ์ผ ๊ฐ์ ๋ฐ์ ๋ ์ฌ์ฉ๋๋ค.
- HandlerMethodArgumentResolver ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ RequestParamMethodArgumentResolver ํด๋์ค๋ก ์์ฒญ ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ถ์ถํ๊ณ WebDataBinder๋ฅผ ํตํด ์ถ์ถ๋ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ธ๋ฉํ๋ค.
์ฌ์ฉ ์
์ปค์คํ ์ปจํธ๋กค๋ฌ
import org.springframework.web.bind.annotation.*;
@RestController
public class MyController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name = "name", defaultValue = "World") String name) {
// ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ก ๋งคํ
return "Hello, " + name;
}
}
์์ฒญ ์์
GET /greeting?name=John
@PathVariable
์ค๋ช
- @PathVariable์ URL ๊ฒฝ๋ก์ ์ผ๋ถ๋ฅผ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ก ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- ์ฃผ๋ก RESTful ์น ์๋น์ค์์ ์์์ ์๋ณ์๋ฅผ ๊ฒฝ๋ก ๋ณ์๋ก ๋ฐ์ ๋ ์ฌ์ฉ๋๋ค.
- HandlerMethodArgumentResolver ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ PathVariableMethodArgumentResolver ํด๋์ค๋ฅผ ํ์ฉํ์ฌ @ PathVariable ์ ๋ ธํ ์ด์ ์ด ๋ถ์ ๊ฒฝ๋ก ๋ณ์๋ฅผ ์ถ์ถํ๊ณ , WebDataBinder๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ๋ณํํ์ฌ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ธ๋ฉํ๋ค.
์ฌ์ฉ ์
์ปค์คํ ์ปจํธ๋กค๋ฌ
import org.springframework.web.bind.annotation.*;
@RestController
public class MyController {
@GetMapping("/users/{userId}")
public String getUser(@PathVariable String userId) {
// URL ๊ฒฝ๋ก ๋ณ์(userId)๋ฅผ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ก ๋งคํ
return "User ID: " + userId;
}
}
์์ฒญ ์์
GET /users/123
@ModelAttribute
์ค๋ช
- @ModelAttribute๋ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ฒด์ ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- ์ฃผ๋ก ํผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํํ๊ฑฐ๋, ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ ๋ ์ฌ์ฉ๋๋ค.
- ๊ฐ์ฒด์ ํ๋์ ์์ฒญ ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ ๋งคํํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- HandlerMethodArgumentResolver ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ ModelAttributeMethodProcessor ํด๋์ค๋ฅผ ์ฌ์ฉํด, @ModelAttribute๊ฐ ์ ์ฉ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ์ถํ๋ค. WebDataBinder ๋ฅผ ์ฌ์ฉํ์ฌ ์ถ์ถ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์์ฑ๋ ๊ฐ์ฒด์ ํ๋์ ๋ฐ์ธ๋ฉํ๋ค.
์ฌ์ฉ ์
์ปค์คํ ์ปจํธ๋กค๋ฌ
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
@Controller
public class MyController {
// ์ด ๋ฉ์๋๋ MyController ๋ด์ ๋ชจ๋ ์์ฒญ ์ ์ ์คํ๋๋ฉฐ ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ค.
@ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("globalAttribute", "This is a global attribute");
}
@PostMapping("/form")
public String submitForm(@ModelAttribute User user) {
// ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ User ๊ฐ์ฒด๋ก ๋งคํ
return "Submitted user with name: " + user.getName();
}
}
@ModelAttribute ์ด๋ ธํ ์ด์ ์ ์ปจํธ๋กค๋ฌ ๋ฉ์๋์ ์ ์ฉ๋์ด, @ModelAttribute ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ถ๊ฐ๋ ๋ฐ์ดํฐ๋ ํด๋น ์ปจํธ๋กค๋ฌ ๋ด์ ๋ชจ๋ ์์ฒญ์ ๋ํด ๋ชจ๋ธ์ ์๋์ผ๋ก ํฌํจ๋ฉ๋๋ค . ์ด ๋ฐฉ๋ฒ์ ์ฃผ๋ก ๋ชจ๋ธ ๋ฐ์ดํฐ๋ฅผ ์ด๊ธฐํํ๊ฑฐ๋, ๋ทฐ์ ๊ณตํต ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋ ์ ์ฉํ๋ค.
์์๋ฅผ ๋ณด๋ฉด @ModelAttribute ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ๋ฉ์๋๋ MyController์ ๋ชจ๋ ์์ฒญ์ ๋ํด ๋ชจ๋ธ์ globalAttribute๋ฅผ ์ถ๊ฐํ๋ค.
User ํด๋์ค
public class User {
private String name;
private String email;
// getters and setters
}
์์ฒญ ์์ (ํผ ๋ฐ์ดํฐ)
POST /form
Content-Type: application/x-www-form-urlencoded
name=John&email=john.doe@example.com
์ฃผ์ ์ฐจ์ด์ ์์ฝ
์ด๋ ธํ ์ด์ | ์ค๋ช | ์ฃผ์ ์ฌ์ฉ ์ฌ๋ก |
@RequestBody | HTTP ์์ฒญ ๋ณธ๋ฌธ์ ๊ฐ์ฒด๋ก ๋ณํ | JSON, XML ๋ฑ์ผ๋ก ์ ์ก๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ |
@RequestParam | HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ก ๋งคํ | ์ฟผ๋ฆฌ ์คํธ๋ง, ํผ ๋ฐ์ดํฐ์ ๋จ์ผ ํ๋ผ๋ฏธํฐ ์ฒ๋ฆฌ |
@ModelAttribute | HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํ ๋ฐ ๋ชจ๋ธ์ ๋ฐ์ดํฐ ์ถ๊ฐ | ํผ ๋ฐ์ดํฐ ๊ฐ์ฒด ๋ณํ, ๋ชจ๋ธ ๋ฐ์ดํฐ ์ถ๊ฐ, ๋ณต์ ํ๋ผ๋ฏธํฐ์ Query String ์ฒ๋ฆฌ๋ ๊ฐ๋ฅ |
@PathVariable | URL ๊ฒฝ๋ก์ ์ผ๋ถ๋ฅผ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ก ๋งคํ | ๊ฒฝ๋ก ๋ณ์๋ก ์์ ์๋ณ์ ์ ๋ฌ |
๊ฒฐ๋ก
@RequestBody, @RequestParam, @ModelAttribute, @PathVariable๋ ๊ฐ๊ฐ HTTP ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. @RequestBody๋ ์์ฒญ ๋ณธ๋ฌธ์ ๊ฐ์ฒด๋ก ๋ณํํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, @RequestParam์ ๋จ์ผ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. @ModelAttribute๋ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํํ๊ณ , ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. @PathVariable์ URL ๊ฒฝ๋ก์ ์ผ๋ถ๋ฅผ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ก ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
'Framework > Spring Framework' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JPA ์ํฐํฐ ์ํ (์๋ช ์ฃผ๊ธฐ) (0) | 2024.06.06 |
---|---|
ํํฐ์ ์ธํฐ์ ํฐ์ ์ฐจ์ด (0) | 2024.03.04 |
Spring Boot 3.X migration ์ด์ (feat. ์คํ๋ง ์ํ๋ฆฌํฐ, JWT ๋ณ๊ฒฝ์ ) (0) | 2023.11.09 |
JPA ์ Spring data JPA ์ฐจ์ด์ (+ ํ์ด๋ฒ๋ค์ดํธ) (0) | 2023.11.08 |
์์ฒญ์ด ์์ ๋ ์คํ๋ง MVC ๋ด๋ถ์ ํ๋ฆ ๊ณผ์ (DispatcherServlet ์ค์ฌ) (0) | 2023.10.31 |
๋๊ธ