[์ฐธ๊ณ ๊ฐ์] ๊น์ํ๋์ ์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์
ํธ๋ฆฌํ ์ถ์ฝ ์ ๋ ธํ ์ด์
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@GetMapping("/mapping/users/{userId}/orders/{orderId}")
public String mappingPath(@PathVariable String userId, @PathVariable Long orderId) {
log.info("mappingPath userId={}, orderId={}", userId, orderId);
return "ok";
}
์คํ ์ํฌ๋ http://localhost:8080/mapping-param?mode=debug
mode=debug ์กฐ๊ฑด์ด ๊ผญ ๋ถ์ด์ผํจ
@GetMapping(value = "/mapping-param", params = "mode=debug")
public String mappingParam() {
log.info("mappingParam");
return "ok";
}
ํค๋์ ํน์ ์กฐ๊ฑด์ด ํ์๋ก ๋ค์ด๊ฐ์ผํจ
@GetMapping(value = "/mapping-header", headers = "mode=debug")
public String mappingHeader() {
log.info("mappingHeader");
return "ok";
}
HTTP ์์ฒญ Content-Type, consume
@PostMapping(value = "/mapping-consume", consumes = MediaType.APPLICATION_JSON_VALUE)
public String mappingConsumes() {
log.info("mappingConsumes");
return "ok";
}
@PostMapping(value = "/mapping-produce", produces = MediaType.TEXT_HTML_VALUE)
public String mappingProduces() {
log.info("mappingProduces");
@RestController
@RequestMapping("/mapping/users")
public class MappingClassController {
@GetMapping
public String user() {
return "get users";
}
@PostMapping
public String addUser() {
return "post user";
}
@GetMapping("/{userId}")
public String findUser(@PathVariable String userId) {
return "get userId = " + userId;
}
@PatchMapping("/{userId}")
public String updateUser(@PathVariable String userId) {
return "update userId = " + userId;
}
@DeleteMapping("/{userId}")
public String deleteUser(@PathVariable String userId) {
return "delete userId = " + userId;
}
}
/mapping/users
/mapping/users
/mapping/users/id1
/mapping/users/id1
/mapping/users/id1
HTTP ํค๋ ์ ๋ณด๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ
@Slf4j
@RestController
public class RequestHeaderController {
@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";
}
}
HttpMethod
: HTTP ๋ฉ์๋๋ฅผ ์กฐํํ๋ค.Locale
: Locale ์ ๋ณด๋ฅผ ์กฐํํ๋ค@RequestHeader MultiValueMap<String, String> headerMap
@RequestHeader("host") String host
@CookieValue(value = "myCookie", required = false) String cookie
์ฐธ๊ณ 1)
MultiValueMap
- Map๊ณผ ์ ์ฌํ๋ฐ, ํ๋์ ํค์ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ ์ ์๋ค.
- HTTP header, HTTP ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด ํ๋์ ํค์ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ ๋ ์ฌ์ฉํ๋ค.
- keyA=value1&keyA=value2
MultiValueMap<String, String> map = new LinkedMultiValueMap(); map.add("keyA", "value1"); map.add("keyA", "value2"); //[value1,value2] List<String> values = map.get("keyA");
์ฐธ๊ณ 2)
@Slf4j
๋ค์ ์ฝ๋๋ฅผ ์๋์ผ๋ก ์์ฑํด์ log๋ฅผ ์ ์ธํด์ค๋ค.private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RequestHeaderController.class);
ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ ์ฃผ๋ก ๋ค์ 3๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
GET ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ ์ก ๋ฐฉ์์ด๋ , POST HTML Form ์ ์ก ๋ฐฉ์์ด๋ ๋๋ค ํ์์ด ๊ฐ์ผ๋ฏ๋ก ๊ตฌ๋ถ์์ด ์กฐํํ ์ ์๋ค.
์ด๊ฒ์ ๊ฐ๋จํ ์์ฒญ ํ๋ผ๋ฏธํฐ ์กฐํ๋ผ ํ๋ค.
@ResponseBody
@RequestMapping("/request-param-v2")
public String requestParamV2(
@RequestParam("username") String memberName,
@RequestParam("age") int memberAge) {
log.info("username={}, age={}", memberName, memberAge);
return "ok";
}
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";
}
String, int ๋ฑ์ ๋จ์ ํ์ ์ด๋ฉด @RequestParam๋ ์๋ต ๊ฐ๋ฅ
์ฃผ์)
@RequestParam ์ ๋ ธํ ์ด์ ์ ์๋ตํ๋ฉด ์คํ๋ง MVC๋ ๋ด๋ถ์์ required=false ๋ฅผ ์ ์ฉํ๋ค.
@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
@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";
}
username ํ๋ผ๋ฏธํฐ๊ฐ ํ์๋ก ๋ค์ด๊ฐ์ผํ๋ค.
int age -> null์ int์ ์
๋ ฅํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค.
๋ฐ๋ผ์ Integer ๋ณ๊ฒฝํด์ผ ํจ. ๋๋ defaultValue ์ฌ์ฉ
/request-param?username=
ํ๋ผ๋ฏธํฐ ์ด๋ฆ๋ง ์๊ณ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ โก๏ธ ๋น๋ฌธ์๋ก ํต๊ณผ
@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(
@RequestParam(defaultValue="guest") String username,
@RequestParam(defaultValue="-1") int age) {
log.info("username={}, age={}", username, age);
return "ok";
}
defaultValue๋ ๋น ๋ฌธ์์ ๊ฒฝ์ฐ์๋ ์ค์ ํ ๊ธฐ๋ณธ ๊ฐ์ด ์ ์ฉ๋๋ค.
@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";
}
์ค์ ๊ฐ๋ฐ์ ํ๋ฉด ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์์ ํ์ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ๊ทธ ๊ฐ์ฒด์ ๊ฐ์ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค.
์คํ๋ง์ ์๋ ์ฝ๋๋ฅผ ์์ ํ ์๋ํํด์ฃผ๋ @ModelAttribute ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค
@RequestParam String username;
@RequestParam int age;
HelloData data = new HelloData();
data.setUsername(username);
data.setAge(age);
์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ธ๋ฉ ๋ฐ์ ๊ฐ์ฒด
@Data
public class HelloData {
private String username;
private int age;
}
๋กฌ๋ณต @Data :
@Getter , @Setter , @ToString , @EqualsAndHashCode , @RequiredArgsConstructor ๋ฅผ ์๋์ผ๋ก ์ ์ฉํด์ค๋ค.
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData) {
log.info("username={}, age={}", helloData.getUsername(),
helloData.getAge());
return "ok";
}
@ModelAttribute๋ ์๋ตํ ์ ์๋ค.
์คํ๋ง์ ์๋ต์ ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ ์ ์ฉํ๋ค.
String , int , Integer ๊ฐ์ ๋จ์ ํ์ = @RequestParam
๋๋จธ์ง = @ModelAttribute (argument resolver ๋ก ์ง์ ํด๋ ํ์ ์ธ)