180112 Spring @RequestMapping - RYUDONGJIN/Memo_wiki GitHub Wiki

@RequestMapping

  • URL์„ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์–ด๋…ธํ…Œ์ด์…˜
  • ํด๋ž˜์Šค๋‚˜ ๋ฉ”์†Œ๋“œ ์„ ์–ธ๋ถ€์— @RequestMapping๊ณผ ํ•จ๊ป˜ URL์„ ๋ช…์‹œํ•˜์—ฌ ์‚ฌ์šฉ
  • ๋ฉ”์†Œ๋“œ ๋ ˆ๋ฒจ์—์„œ ์ •์˜ํ•œ @RequestMapping์€ ํƒ€์ž… ๋ ˆ๋ฒจ์—์„œ ์ •์˜๋˜์–ด @RequestMapping์˜ ์˜ต์…˜์„ ์ƒ์†๋ฐ›์Œ
  • ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ viewName์„ ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด @RequestMapping์˜ path๋กœ ์„ค์ •ํ•œ URL์ด ๊ทธ๋Œ€๋กœ viewName์œผ๋กœ ์„ค์ •

options

path(ํ˜น์€ value) : ์š”์ฒญ๋œ URL์— ๋”ฐ๋ผ ๋งคํ•‘

@RequestMapping(path = { "/addMovie.do", "/updateMovie.do" }) 
public String myMethod() {
    // "/addMovie.do", "/updateMovie.do" ๋‘ URL ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ๋‹ค.
}

์†์„ฑ๋ช…์„ ์ƒ๋žตํ•˜๊ณ  @RequestMapping("/addMovie.do")์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

method : GET, POST, PUT, DELETE ๊ฐ™์€ HTTP Request method์— ๋”ฐ๋ผ ๋งคํ•‘์„ ๊ฒฐ์ •. ๊ฐ’์€ enum ํƒ€์ž…์ธ RequestMethod.

@RequestMapping(method = RequestMethod.POST)
public String myMethod() {
    // ...
}

params : ์š”์ฒญ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋”ฐ๋ผ ๋งคํ•‘

@RequestMapping(params = {"param1=a", "param2", "!myParam"})
public String myMethod() {
    // ...
}

headers : ํŠน์ • ํ—ค๋”์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋งคํ•‘. ์™€์ผ๋“œ ์นด๋“œ ํ‘œํ˜„(*)๋„ ์ง€์›

@RequestMapping(path = "/movie.do", headers = "content-type=text/*") 
public String myMethod() {
    // ...
}

HTTP Request์— Content-Type ํ—ค๋” ๊ฐ’์ด "text/html", "text/plain" ๋ชจ๋‘ ๋งค์นญ์ด ๋œ๋‹ค.

produces TODO

@RequestMapping(path = "...", produces = "application/json")
@RequestBody
public HashMap<String, String> testMethod(Model model) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("code", "0");
    return map;
}