
@Slf4j
@RestController
public class RestHeaderController {
@RequestMapping("/headers")
public String headers(HttpServletRequest request, HttpServletResponse response,
HttpMethod httpMethod,
Locale locale,
@RequestHeader MultiValueMap<String, String> headerMap,
@RequestHeader("host") String host,
@CookieValue(value = "myCookie", required = false) String cookie) {
log.info("request={}", request);
log.info("response={}", response);
log.info("httpMethod={}", httpMethod);
log.info("locale={}", locale);
log.info("headerMap={}", headerMap);
log.info("header host={}", host);
log.info("myCookie={}", cookie);
return "ok";
}
}
- HttpServletRequest
HttpServletResponseHttpMethod: HTTP ๋ฉ์๋๋ฅผ ์กฐํํ๋ค.org.springframework.http.HttpMethodLocale: Locale ์ ๋ณด๋ฅผ ์กฐํํ๋ค.@RequestHeader MultiValueMap<String, String> headerMap
- ๋ชจ๋ HTTP ํค๋๋ฅผ
MultiValueMapํ์์ผ๋ก ์กฐํํ๋ค.@RequestHeader("host") String host
- ํน์ HTTP ํค๋๋ฅผ ์กฐํํ๋ค.
- ์์ฑ
- ํ์ ๊ฐ ์ฌ๋ถ :
required- ๊ธฐ๋ณธ ๊ฐ ์์ฑ :
defaultValue@CookieValue(value = "myCookie", required = false) String cookie
- ํน์ ์ฟ ํค๋ฅผ ์กฐํํ๋ค.
- ์์ฑ
- ํ์ ๊ฐ ์ฌ๋ถ :
required- ๊ธฐ๋ณธ ๊ฐ :
defaultValue
keyA=value1&keyA=value2 > key A์ 2๊ฐ์ value๊ฐ ๋ด๊น MultiValueMap<String, String> map = new LinkedMultiValueMap();
map.add("keyA", "value1");
map.add("keyA", "value2");
//[value1,value2]
List<String> values = map.get("keyA");
log๋ฅผ ์ฌ์ฉํ๊ฒ ํด์ฃผ๋ ๊ฒ์ ๋์์ค๋ค.private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(RequestHeaderController.class);
์ฐธ๊ณ
@Controller์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก์ ์๋์ ๊ณต์ ๋ฉ๋ด์ผ ํ์ธ- https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/arguments.html
- ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ ์ฃผ๋ก ๋ค์ 3๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉ
- /url?username=hello&age=20
- ๋ฉ์์ง ๋ฐ๋ ์์ด, URL์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ์ ๋ฌ
- EX) ๊ฒ์, ํํฐ, ํ์ด์ง๋ฑ์์ ๋ง์ด ์ฌ์ฉํ๋ ๋ฐฉ์
- POST - HTML Form
- content-type : application/x-www-form-urlencoded
- ๋ฉ์์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌ username=hello&age=20
- ์) ํ์ ๊ฐ์ , ์ํ ์ฃผ๋ฌธ, HTML Form ์ฌ์ฉ
- HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญ
- HTTP API์์ ์ฃผ๋ก ์ฌ์ฉ, JSON, XML, TEXT
- ๋ฐ์ดํฐ ํ์์ ์ฃผ๋ก JSON ์ฌ์ฉ
- POST, PUT, PATCH
HttpServletRequest์ request.getParameter()๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค์ ๋๊ฐ์ง ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์กฐํํ ์ ์๋ค.
GET, ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ ์ก
http://localhost:8080/request-param?username=hello&age=20POST, HTML Form ์ ์ก
POST /request-param ...
content-type: application/x-www-form-urlencoded
username=hello&age=20 GET ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ ์ก ๋ฐฉ์์ด๋ , POST HTML Form ์ ์ก ๋ฐฉ์์ด๋ ๋๋ค ํ์์ด ๊ฐ์ผ๋ฏ๋ก ๊ตฌ๋ถ์์ด ์กฐํํ ์ ์๋ค.
/**
* ๋ฐํ ํ์
์ด ์์ผ๋ฉด์ ์ด๋ ๊ฒ ์๋ต์ ๊ฐ์ ์ง์ ๋ฃ์ผ๋ฉด, view ์กฐํ X
*/
@RequestMapping("/request-param-v1")
public void requestParamV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
log.info("username:{}, age:{}", username, age);
response.getWriter().write("ok");
}
/**
* @RequestParam ์ฌ์ฉ
* - ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ผ๋ก ๋ฐ์ธ๋ฉ
* @ResponseBody ์ถ๊ฐ
* - View ์กฐํ๋ฅผ ๋ฌด์ํ๊ณ , HTTP message body์ ์ง์ ํด๋น ๋ด์ฉ ์
๋ ฅ
*/
@ResponseBody
@RequestMapping("/request-param-v2")
public String requestParamV2(@RequestParam("username") String memberName, @RequestParam("age") int memberAge) {
log.info("username={}, age={}", memberName, memberAge);
return "ok";
}
@RequestParam : ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ผ๋ก ๋ฐ์ธ๋ฉ@ResponseBody : View ์กฐํ๋ฅผ ๋ฌด์ํ๊ณ , HTTP message body์ ์ง์ ํด๋น ๋ด์ฉ ์
๋ ฅRequestParam์ name(value) ์์ฑ์ด ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ผ๋ก ์ฌ์ฉ
/**
* @RequestParam ์ฌ์ฉ
* HTTP ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๋ณ์ ์ด๋ฆ๊ณผ ๊ฐ์ผ๋ฉด @RequestParam(name="xx") ์๋ต ๊ฐ๋ฅ
*/
@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(
@RequestParam String username,
@RequestParam int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@RequestParam(name="xx")์๋ต ๊ฐ๋ฅ/**
* @RequestParam ์ฌ์ฉ
* String, int ๋ฑ์ ๋จ์ ํ์
์ด๋ฉด @RequestParam ๋ ์๋ต ๊ฐ๋ฅ
*/
@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
String, int, Integer ๋ฑ์ ๋จ์ ํ์
์ด๋ฉด @RequestParam ๋ ์๋ต ๊ฐ๋ฅ์ฃผ์
@RequestParam์ ๋ ธํ ์ด์ ์ ์๋ตํ๋ฉด ์คํ๋ง MVC๋ ๋ด๋ถ์์required=false๋ฅผ ์ ์ฉํ๋ค.
์ฐธ๊ณ
- ์ ๋ ธํ ์ด์ ์ ์์ ํ ์๋ตํด๋ ์๊ด์ ์์ง๋ง, ๋๋ฌด ์๋ ๊ฒ๋ ์ฝ๊ฐ ๊ณผํ๋ค๋ ์ด์ผ๊ธฐ๋ฅผ ๋ค์๋ค.
@RequestParam์ด ์์ผ๋ฉด ๋ช ํํ๊ฒ ์์ฒญ ํ๋ผ๋ฏธํฐ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ๋ค๋ ๊ฒ์ ํ์ ํ ์ ์๋ค.- ์คํ๋ง์ ์ต์ํ์ง ์์ ์ฌ๋์ผ ๊ฒฝ์ฐ ๋ณด์๋ง์ ์ดํด๋ฅผ ํ์ง๋ชปํ ์๋ ์์ผ๋ ์ฃผ์ํด์ ์ฌ์ฉํ์.
/**
* @RequestParam.required /request-param-required -> username์ด ์์ผ๋ฏ๋ก ์์ธ
* <p>
* ์ฃผ์!
* /request-param-required?username= -> ๋น๋ฌธ์๋ก ํต๊ณผ
* <p>
* ์ฃผ์!
* /request-param-required
* int age -> null์ int์ ์
๋ ฅํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅ, ๋ฐ๋ผ์ Integer ๋ณ๊ฒฝํด์ผ ํจ(๋๋ ๋ค์์ ๋์ค๋
* defaultValue ์ฌ์ฉ)
*/
@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(
@RequestParam(required = true) String username,
@RequestParam(required = false) Integer age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@RequestParam.required/request-param-required ์์ฒญ์ฃผ์! ํ๋ผ๋ฏธํฐ ์ด๋ฆ๋ง ์ฌ์ฉ
/request-param-required?username=์ฃผ์! - ๊ธฐ๋ณธํ(primitive)์ null ์ ๋ ฅ
/request-param์์ฒญ@RequestParam(required = false) int agenull์ int์ ์ด๋ณ
ํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅ (500 ์์ธ ๋ฐ์) ๋ฐ๋ผ์ null์ ๋ฐ์ ์ ์๋ Integer๋ก ๋ณ๊ฒฝํ๊ฑฐ๋, ๋๋ defaultValue๋ฅผ ์ฌ์ฉํ์./**
* @RequestParam
* - defaultValue ์ฌ์ฉ
*
* ์ฐธ๊ณ : defaultValue๋ ๋น ๋ฌธ์์ ๊ฒฝ์ฐ์๋ ์ ์ฉ
* /request-param-default?username=
*/
@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(
@RequestParam(required = true, defaultValue = "guest") String username,
@RequestParam(required = false, defaultValue = "-1") int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
defaultValue๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธฐ๋ณธ ๊ฐ์ ์ ์ฉํ ์ ์๋ค. ์ด๋ฏธ ๊ธฐ๋ณธ ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ required๋ ์๋ฏธ๊ฐ ์๋ค.defaultValue๋ ๋น ๋ฌธ์์ ๊ฒฝ์ฐ์๋ ์ค์ ํ ๊ธฐ๋ณธ ๊ฐ์ด ์ ์ฉ๋๋ค./request-param-default?username์ด๋ฐ์์ผ๋ก ๋ณด๋ด๋ฉด default๊ฐ์ด ์ ์ฉ๋์ดrequest๋ฅผ ๋ณด๋ด๊ฒ ๋๋ค.
/**
* @RequestParam Map, MultiValueMap
* Map(key=value)
* MultiValueMap(key=[value1, value2, ...]) ex) (key=userIds, value=[id1, id2])
*/
@ResponseBody
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
log.info("username={}, age={}", paramMap.get("username"),
paramMap.get("age"));
return "ok";
}
@RequestParam Map, Map(key=value)@RequestParam MultiValueMapMultiValueMap(key=[value1, value2, ... ] ex) (key=userIds, value=[id1, id2])ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด 1๊ฐ๊ฐ ํ์คํ๋ค๋ฉด Map์ ์ฌ์ฉํด๋ ๋์ง๋ง, ๊ทธ๋ ์ง ์๋ค๋ฉด MultiValueMap์ ์ฌ์ฉํ์. ํ์ง๋ง ๊ฑฐ์ 1๊ฐ์ผ ๊ฒฝ์ฐ๊ฐ ๋ง๊ธฐ ๋๋ฌธ์ Map์ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
@ModelAttribute ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.์ค์ต์ ์์ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ธ๋ฉ ๋ฐ์ ๊ฐ์ฒด ์์ฑ
//HelloData
import lombok.Data;
@Data
public class HelloData {
private String username;
private int age;
}
@Data@Getter, @Setter,@ToString, @EqualsAndHashCode, @RequiredArgsConstructor ๋ฅผ ์๋์ผ๋ก ์ ์ฉํด์ค๋ค./**
* @ModelAttribute ์ฌ์ฉ
* ์ฐธ๊ณ : model.addAttribute(helloData) ์ฝ๋๋ ํจ๊ป ์๋ ์ ์ฉ๋จ, ๋ค์ model์ ์ค๋ช
ํ ๋ ์์ธํ
์ค๋ช
*/
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData) {
log.info("username ={}, age={}", helloData.getUsername(), helloData.getAge());
return "ok";
}
์คํํด๋ณด๋ฉด ์๋์ผ๋ก HelloData ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ , ์์ฒญ ํ๋ผ๋ฏธํฐ์ ๊ฐ๋ ๋ชจ๋ ๋ค์ด๊ฐ ์๋๊ฒ์ ํ์ธํ ์ ์๋ค.
์คํ๋ง MVC๋ @ModelAttribute๊ฐ ์์ผ๋ฉด ๋ค์์ ์คํํ๋ค.
HelloData๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.HelloData๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐพ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋น ํ๋กํผํฐ์ setter๋ฅผ ํธ์ถํด์ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ์
๋ ฅ(๋ฐ์ธ๋ฉ)ํ๋ค.username์ด๋ฉด setUsername() ๋ฉ์๋๋ฅผ ์ฐพ์์ ํธ์ถํ๋ฉด์ ๊ฐ์ ์
๋ ฅgetUsername(), setUsername() ๋ฉ์๋๊ฐ ์์ผ๋ฉด, ์ด ๊ฐ์ฒด๋username ์ด๋ผ๋ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์์.username ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด setUsername()์ด ํธ์ถ๋๊ณ , ์กฐํํ๋ฉด getUsername()์ด ํธ์ถ๋๋ค.class HelloData{
getUsername();
setUsername();
}
age=abc ์ฒ๋ผ ์ซ์๊ฐ ๋ค์ด๊ฐ์ผ ํ ๊ณณ์ ๋ฌธ์๋ฅผ ๋ฃ์ผ๋ฉด BindException๋ฐ์/**
* @ModelAttribute ์๋ต ๊ฐ๋ฅ
* String, int ๊ฐ์ ๋จ์ ํ์
= @RequestParam
* argument resolver ๋ก ์ง์ ํด๋ ํ์
์ธ = @ModelAttribute
*/
@ResponseBody
@RequestMapping("/model-attribute-v2")
public String modelAttributeV2(HelloData helloData) {
log.info("username={}, age={}", helloData.getUsername(),
helloData.getAge());
return "ok";
}
์คํ๋ง์ ํด๋น ์๋ต์ ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ ์ ์ฉ
String, int, Integer ๊ฐ์ ๋จ์ ํ์
= @RequestParam@ModelAttribute (argument resolver๋ก ์ง์ ํด๋ ํ์
์ธ)