Bibi's DevLog ๐Ÿค“๐ŸŽ

@RequestMapping ์œผ๋กœ ์ค‘๋ณตURL ์ œ๊ฑฐํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

@RequestMapping ์œผ๋กœ ์ค‘๋ณตURL ์ œ๊ฑฐํ•˜๊ธฐ

๋น„๋น„ bibi 2021. 3. 17. 00:29

@RequestMapping

์‚ฌ์šฉ๋ฒ• ๊ณต๋ถ€ํ•ด์„œ ์ค‘๋ณตURL ์ œ๊ฑฐํ•˜์ž.

์ฐธ๊ณ  : ๋ธ”๋กœ๊ทธ. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

@RequestMapping ์–ด๋…ธํ…Œ์ด์…˜์€

HTTP request๋กœ ๋“ค์–ด์˜ค๋Š” URL์„ ํŠน์ • ์ปจํŠธ๋กค๋Ÿฌ์˜ ํด๋ž˜์Šค ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. (์ปจํŠธ๋กค๋Ÿฌ์˜ ํด๋ž˜์Šค ๋ฐ ํŠน์ • ๋ฉ”์†Œ๋“œ ๋ชจ๋‘ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค)

์‚ฌ์‹ค์€ URL ๋ฟ ์•„๋‹ˆ๋ผ ํŒŒ๋ผ๋ฏธํ„ฐ, ํ—ค๋” ๋“ฑ ์—ฌ๋Ÿฌ ๋ฒ”์œ„์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•ด ๋‚˜์ค‘์— ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.

@RequestMapping ๋ฅผ ํด๋ž˜์Šค์— ์„ ์–ธํ•˜๋ฉด ์ค‘๋ณตURL์„ ์ œ๊ฑฐํ•  ๋•Œ ์ข‹๋‹ค.

์•„๋ž˜๋Š” ์˜ˆ์ œ.

@Controller
@RequestMapping("/home")
public class TestController {
    @RequestMapping("/")
    String getName(){
        return "Hello from getName() method";
    }
    // http://localhost:8080/home/์€ getName()์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.

    @RequestMapping("/info")
    String showInfo(){
        return "Hello from showInfo() method";
    }
    // http://localhost:8080/home/info์€ showInfo()์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.
}