์ ๋ ธํ ์ด์ ๊ธฐ๋ฐ์ ์คํ๋ง ์ปจํธ๋กค๋ฌ๋ ๋ค์ํ ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ํ๋ค.
- ํค๋ ์ ๋ณด ์กฐํ
- ์์ฒญ ํ๋ผ๋ฏธํฐ ์กฐํ
(1) ์์ฒญ ํ๋ผ๋ฏธํฐ ์ง์ ์กฐํ(@RequestParam)
(2) ๊ฐ์ฒด๋ฅผ ์ด์ฉํ ์กฐํ(@ModelAttribute) - ์์ฒญ ๋ฉ์์ง ์กฐํ
(1) ๋จ์ ํ ์คํธ (@RequestBody)
(2) JSON
ํ๋์ฉ ์์ธํ ์์๋ณด์.
1. ํค๋ ์ ๋ณด ์กฐํ ๐
HTTP ํค๋ ์ ๋ณด๋ฅผ ์๋์ ๊ฐ์ด ์กฐํํ ์ ์๋ค.
@RequestMapping("/headers")
public String headers(HttpServletRequest request,
HttpServletRequest response,
HttpMethod httpMethod,
Locale locale,
@RequestHeader MultiValueMap<String, String> headerMap,
@RequestHeader("host") String host,
@CookieValue(value = "myCookie", required = false) String cookie
) {
return "home";
}
- HttpServletRequest
- HttpServletResponse
- HttpMethod
- Locale
Locale ์ ๋ณด๋ฅผ ์กฐํํ๋ค.
Locale ์ ๋ณด๋ 'ko_KR'๊ณผ ๊ฐ์ด ์ธ์ด์ ๋ณด๋ฅผ ๋ปํ๋ค. - @RequestHeader MultiValueMap<String, String> headerMap
๋ชจ๋ HTTP ํค๋๋ฅผ MultiValueMap ํ์์ผ๋ก ๊ฐ์ ธ์จ๋ค.
MultiValueMap์ด๋ ํ๋์ ํค์ ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ง ์ ์๋ ๊ฒ์ด๋ค. - @RequestHeader("host") String host
ํน์ HTTP ํค๋๋ฅผ ์กฐํํ๋ค. - @CookieValue(value = "myCookie", required = false) String cookie
ํน์ ์ฟ ํค ์ ๋ณด๋ฅผ ์กฐํํ๋ค.
2. ํ๋ผ๋ฏธํฐ ์กฐํ - ์ง์ ์กฐํ(@RequestParam) ๐
ํ๋ผ๋ฏธํฐ๋ฅผ 2๊ฐ์ง๋ก ๋๋ ์ ์๋ค.
- ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ(GET)
๋ฉ์์ง ๋ฐ๋ ์์ด, URL์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ์ ๋ฌํ๋ ๊ฒ
/url?username=amenable&age=20 - HTML Form(POST)
๋ฉ์์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌํ๋ ๊ฒ (username=amenable&age=20)
content-type: application/x-www-form-urlencoded
๋ ๊ฐ์ง ๋ชจ๋ ์๋์ ๋ฐฉ์๋ค๋ก ์กฐํํ ์ ์๋ค.
V1. HttpServletRequest์ request.getParameter()
@RequestMapping("/request-param-v1")
public String requestParamV1(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
return "home";
}
V2. @RequestParam
@RequestMapping("/request-param-v2")
public String requestParamV2(@RequestParam("username") String memberName,
@RequestParam("age") int memberAge) {
return "home";
}
ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ผ๋ก ๋ฐ์ธ๋ฉ์ ํ๋ค.
V3. @RequestParam ์ธ์ ์๋ต
@RequestMapping("/request-param-v3")
public String requestParamV3(@RequestParam String username,
@RequestParam int age) {
return "home";
}
HTTP ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๋ณ์ ์ด๋ฆ๊ณผ ๊ฐ์ผ๋ฉด @RequestParam์ ์ธ์๋ฅผ ์๋ตํ ์ ์๋ค. ์๋๋ @RequestParam("username")์ธ๋ฐ ๋ณ์ ์ด๋ฆ์ username์ผ๋ก ํด์คฌ๊ธฐ ๋๋ฌธ์ ์๋ต์ด ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
V4. @RequestParam ์๋ต
@RequestMapping("/request-param-v4")
public String requestParamV4(String username,
int age) {
return "home";
}
String, int, Integer ๋ฑ์ ๋จ์ ํ์ ์ด๋ฉด @RequestParam ์์ฒด๋ ์๋ต์ด ๊ฐ๋ฅํ๋ค.
ํ์ง๋ง @RequestParam์ด ์๋ตํ์ง ์์ผ๋ฉด, ๋ช ํํ๊ฒ ์์ฒญ ํ๋ผ๋ฏธํฐ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค๋ ์ฅ์ ์ด ์๊ธด ํ๋ค.
+α. Map์ ์ด์ฉํ ์กฐํ
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
Object username = paramMap.get("username");
Object age = paramMap.get("age");
return "home";
}
ํ๋ผ๋ฏธํฐ๋ฅผ Map๊ณผ MultiValueMap์ผ๋ก ์กฐํํ ์๋ ์๋ค.
3. ํ๋ผ๋ฏธํฐ ์กฐํ - ๊ฐ์ฒด๋ฅผ ์ด์ฉํ ์กฐํ(@ModelAttribute) ๐
์ง๊ธ๊น์ง๋ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ๋ฐ๋ ๊ฒ์ ์์๋ณด์๋ค. ํ์ง๋ง ์ค์ ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๋ก ์ฐ๋ ๊ฒ ์๋๋ผ, ํ์ํ ๊ฐ์ฒด์ ์์ฒญ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฃ์ด์ ํด๋น ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ค. ์ด๊ฒ์ ์คํ๋ง์์ ์ ๊ณตํ๋ @ModelAttribute๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉํด ๋ณด์.
V1. @ModelAttribute
@Data
public class Person {
private String name;
private int age;
}
----------
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute Person person) {
return "home";
}
์ฌ์ฉ๋ฒ์ ์๋นํ ๊ฐ๋จํ๋ค. ์์ ์ฝ๋์ฒ๋ผ ์์ฑํด ์ฃผ๋ฉด ์คํ๋ง์ด Person ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ํด๋น ๊ฐ์ฒด์ setter๋ฅผ ์ด์ฉํ์ฌ ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ ๋ ฅ(๋ฐ์ธ๋ฉ)ํ๋ค.
V2. @ModelAttribute ์๋ต
@RequestMapping("/model-attribute-v2")
public String modelAttributeV2(Person person) {
return "home";
}
@ModelAttribute์ ์์ ๊ฐ์ด ์๋ตํ ์ ์๋ค.
์์์ ์ดํด๋ณธ ๊ฒ๊ณผ ๊ฐ์ด @RequestParam๋ ์๋ตํ ์ ์์๋๋ฐ, ์๋ต๊ณผ ๊ด๋ จํด์ ์คํ๋ง์ ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ ๋ฐ๋ฅธ๋ค.
- String, int, Integer ๊ฐ์ ๋จ์ ํ์ ์ธ ๊ฒฝ์ฐ → @RequestParam
- ๋๋จธ์ง → @ModelAttribute
4. ๋ฉ์์ง ์กฐํ - ๋จ์ ํ ์คํธ(@RequestBody) ๐ญ
์์ฒญ ๋ฉ์์ง๋ HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญํ๋ ๊ฒ์ด๋ค. HTTP API์์ ์ฃผ๋ก ์ฌ์ฉ๋๊ณ , ์ข ๋ฅ๋ก๋ JSON, XML, TEXT ๋ฑ์ด ์๋ค. ๋จผ์ ๋จ์ ํ ์คํธ๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด์.
V1. HttpServletResponse + InputStream
@PostMapping("/request-body-string-v1")
public String requestBodyStringV1(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
return "home";
}
HTTP ๋ฉ์์ง ๋ฐ๋์ ๋ฐ์ดํฐ๋ฅผ InputStream์ ์ฌ์ฉํด์ ์ง์ ์ฝ์ ์ ์๋ค.
V2. InputStream
@PostMapping("/request-body-string-v2")
public String requestBodyStringV2(InputStream inputStream) throws IOException {
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
return "home";
}
์คํ๋ง MVC์์ ์ ๊ณตํ๋ InputStream ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ์ฌ HTTP ์์ฒญ ๋ฉ์์ง ๋ฐ๋์ ๋ด์ฉ์ ์ง์ ์กฐํํ ์ ์๋ค. (OutputStream์ ์ด์ฉํด์ ์ง์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ ์๋ ์๋ค.)
V3. HttpEntity
@PostMapping("/request-body-string-v3")
public String requestBodyStringV3(HttpEntity<String> httpEntity) throws IOException {
String messageBody = httpEntity.getBody();
return "home";
}
์คํ๋ง MVC์์ ์ ๊ณตํ๋ HttpEntity ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ์ฌ HTTP header, body ์ ๋ณด๋ฅผ ํธ๋ฆฌํ๊ฒ ์กฐํํ ์ ์๋ค. HttpEntity๋ ๋ฉ์์ง ๋ฐ๋ ์ ๋ณด๋ฅผ ์ง์ ์กฐํํ๋ค.
์ด๋ ์์์ ์ดํด๋ณธ @RequestParam๊ณผ @ModelAttribute์๋ ๊ด๋ จ์ด ์๋ค.
V4. @RequestBody
@PostMapping("/request-body-string-v4")
public String requestBodyStringV4(@RequestBody String messageBody) throws IOException {
return "home";
}
@RequestBody๋ฅผ ์ฌ์ฉํ๋ฉด HTTP ๋ฉ์์ง ๋ฐ๋ ์ ๋ณด๋ฅผ ํธ๋ฆฌํ๊ฒ ์กฐํํ ์ ์๋ค. ๋ง์ฝ ๋ฐ๋๊ฐ ์๋ ํค๋ ์ ๋ณด๊ฐ ํ์ํ๋ค๋ฉด HttpEntity๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ @RequestHeader๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
์ด๊ฒ ๋ํ @RequestParam๊ณผ @ModelAttribute์๋ ๊ด๋ จ์ด ์๋ค.
5. ๋ฉ์์ง ์กฐํ - JSON(@RequestBody) ๐ฟ
์ด๋ฒ์๋ ์์ฒญ ๋ฉ์์ง ์ค์์ HTTP API์์ ์ฃผ๋ก ์ฌ์ฉํ๋ JSON ๋ฐ์ดํฐ ํ์์ ์กฐํํด ๋ณด์.
V1. HttpServletRequest + JSON ๋ณํ
private ObjectMapper objectMapper = new ObjectMapper();
@PostMapping("/request-body-json-v1")
public String requestBodyJsonV1(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
Person person = objectMapper.readValue(messageBody, Person.class);
return "home";
}
HttpServletRequest๋ฅผ ์ฌ์ฉํด์ ์ง์ HTTP ๋ฉ์์ง ๋ฐ๋์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์์ ๋ฌธ์๋ก ๋ณํํ์๋ค. ๋ฌธ์๋ก ๋ JSON ๋ฐ์ดํฐ๋ฅผ Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ ObjectMapper๋ฅผ ์ฌ์ฉํด์ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ์๋ค.
V2. @RequestBody + JSON ๋ณํ
private ObjectMapper objectMapper = new ObjectMapper();
@PostMapping("/request-body-json-v2")
public String requestBodyJsonV2(@RequestBody String messageBody) throws IOException {
Person person = objectMapper.readValue(messageBody, Person.class);
return "home";
}
์ด์ ์ ์ดํด๋ดค๋ @RequestBody๋ฅผ ์ฌ์ฉํ์ฌ HTTP ๋ฉ์์ง์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด๊ณ messageBody์ ์ ์ฅํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฌธ์๋ก ๋ JSON ๋ฐ์ดํฐ์ธ messageBody๋ฅผ objectMapper๋ฅผ ํตํด์ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ๋ค.
V3. @RequestBody
@PostMapping("/request-body-json-v3")
public String requestBodyJsonV3(@RequestBody Person person) throws IOException {
return "home";
}
@RequestBody์ ์ง์ ๊ฐ์ฒด๋ฅผ ์ง์ ํ์ฌ HTTP ๋ฉ์์ง ๋ฐ๋์ ๋ด์ฉ์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฐ์ฒด๋ก ๋ณํํ๋ค.
@RequestBody๋ '@RequestParam๊ณผ @ModelAttribute'์ ๋ฌ๋ฆฌ ์๋ต์ด ๋ถ๊ฐ๋ฅํ๋ค. ๊ทธ ์ด์ ๋ @RequestBody๊ฐ ์๋ต๋๋ค๊ณ ํ๋ฉด @ModelAttribute๊ฐ ์ ์ฉ๋์ด ๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ค.
+α. HttpEntity
@PostMapping("/request-body-json-v4")
public String requestBodyJsonV4(HttpEntity<Person> httpEntity) throws IOException {
Person person = httpEntity.getBody();
return "home";
}
๋ฌผ๋ก JSON์ ์ฒ๋ฆฌํ ๋ ๋จ์ ํ ์คํธ์์ ์ฌ์ฉํ๋ HttpEntity๋ฅผ ์ด์ฉํด๋ ๋๋ค.
ํด๋น ๊ธ์ ๊น์ํ ๋์ '์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ '์ ์ฐธ๊ณ ํ์์ต๋๋ค.
'๐ ์คํ๋ง > ๊ธฐ๋ณธ ๊ฐ๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฉ์์ง & ๊ตญ์ ํ (Message & Internationalization) (0) | 2023.04.25 |
---|---|
HTTP ์๋ต(์ ์ ๋ฆฌ์์ค, ๋ทฐ ํ ํ๋ฆฟ, HTTP ๋ฉ์์ง) ์ฒ๋ฆฌ (0) | 2023.04.21 |
์คํ๋ง MVC (0) | 2023.04.19 |
์์กด์ฑ ์ฃผ์ - DI(Dependency Injection) (0) | 2023.04.18 |
๋ก๊น (SLF4J, Logback) (0) | 2023.04.09 |