Spring

Spring [@RequestMapping ๊ธฐ์ดˆ ์ •๋ฆฌ ]

DAHLIA CHOI 2021. 8. 14. 17:25

 

์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜, ๊ธฐ์ดˆ๊ฐ•์˜๋ฅผ ๋“ค์—ˆ์„ ๋•Œ ์ข…์ข… ๋ณด์•˜๋˜ RequestMapping์— ๋Œ€ํ•œ ๊ธฐ์ดˆ๋ฅผ ๋‹ค์‹œ ๋‹ค์กŒ๋‹ค!

์•„์ง ๊ธฐ์ดˆ๋งŒ ๋ฐฐ์šด ๊ฑฐ๋ผ ์‹ค๋ฌด์—์„œ ์–ด๋””์— ์‚ฌ์šฉํ• ์ง€ ๊ฐ๋ฐ–์— ์˜ค์ง€ ์•Š์ง€๋งŒ ์–ธ์  ๊ฐ„ ๋Šฅ์ˆ˜๋Šฅ๋ž€ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๐Ÿ˜š

 

 

 


RequestMapping(์š”์ฒญ ๋งคํ•‘) ์ด๋ž€?

์š”์ฒญ์ด ์™”์„ ๋•Œ ์–ด๋–ค ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ์ด ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์ง€ํ‘œ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค.

 

๐Ÿ‘‰ ์˜ˆ์‹œ

@RequestMapping(value = "/hello-basic")

 ์ด๋ ‡๊ฒŒ ๋งคํ•‘์„ ํ•˜๋ฉด localhost:8080/hello-basic์œผ๋กœ url์„ ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ์— ์ด๊ฒƒ์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

 

@RequestMapping์€ ๋‹ค์ค‘์š”์ฒญ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค!

๋‹ค์ค‘ ์š”์ฒญ์„ ํ•˜๋ ค๋ฉด ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด์•ผ ํ•œ๋‹ค. 

@RequestMapping(value = {"/hello", "/hello-basic"})

์ด๊ฒƒ์ฒ˜๋Ÿผ ๋‹ค์ค‘ ์š”์ฒญ์„ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋‘˜ ์ค‘ ์•„๋ฌด url์ด๋‚˜ ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค.

 

 

[ ๊ฒฝ๋กœ๋ณ€์ˆ˜(PathVariable) ์‚ฌ์šฉ ]

 

์ตœ๊ทผ HTTP API๋Š” ๋ฆฌ์†Œ์Šค ๊ฒฝ๋กœ์— ์‹๋ณ„์ž๋ฅผ ๋„ฃ๋Š” ์Šคํƒ€์ผ์„ ์„ ํ˜ธํ•œ๋‹ค.

์ฟผ๋ฆฌ ํŒŒ๋งˆ๋ฆฌํ„ฐ๋กœ ์‹๋ณ„์ž๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ๊ฒฝ๋กœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋งคํ•‘๋˜๋Š” ๋ถ€๋ถ„์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ‘‰ ์˜ˆ์‹œ

@GetMapping("/mapping/{userId}")
    public String mappingPath(@PathVariable("userId") String data) {
        log.info("mappingPath userId={}", data);
        return "ok";
    }

 

localhost:8080/mapping/userA
๋ผ๊ณ  ์ž…๋ ฅํ•˜๋ฉด userId๊ฐ€ data๋กœ ๋งค์นญ๋ผ์„œ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ €๋ ‡๊ฒŒ data๋กœ ๊บผ๋‚ด๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ง๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

@GetMapping("mapping/{userId}")
    public String mappingPath(@PathVariable String userId) {
        log.info("mappingPath userId={}", userId);
        return "ok";
    }

 

 

โž• PathVariable ๋‹ค์ค‘ ์‚ฌ์šฉ

@GetMapping("/mapping/users/{userId}/orders/{orderId}")
    public String mappingPath(@PathVariable String userId, @PathVariable Long orderId) {
        log.info("mappingPath userId={}, orderId={}", userId, orderId);
        return "ok";
    }

์ด๋ ‡๊ฒŒ ๋‹ค์ค‘์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค!!!

 

 

 

[ ํŠน์ • ํ—ค๋” ์กฐ๊ฑด ๋งคํ•‘ ]

 

@GetMapping(value = "/mapping-header", headers = "mode=debug")
    public String mappingHeader() {
        log.info("mappingHeader");
        return "ok";
    }

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ์„ ๊ฒฝ์šฐ์—๋Š” localhost:8080/mapping-header์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค!

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” localhost:8080/mapping-header?mode=debug๋กœ ์ด๋™ํ•ด์•ผ ์ •์ƒ์ ์ธ ํŽ˜์ด์ง€๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

[ ๋ฏธ๋””์–ด ํƒ€์ž… ๋งคํ•‘ ]

 

HTTP ์š”์ฒญ์˜ Content-Type ํ—ค๋”๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฏธ๋””์–ด ํƒ€์ž…์œผ๋กœ ๋งคํ•‘ํ•œ๋‹ค.

๋งŒ์•ฝ ๋งž์ง€ ์•Š์œผ๋ฉด HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ‘‡ ์—ฌ๊ธฐ์— Content-Type์— ๊ด€ํ•œ ๋‚ด์šฉ์ด ๊ฐ„๋žตํ•˜๊ฒŒ ์š”์•ฝ๋˜์–ด์žˆ๋‹ค!

 

HTTP ํ—ค๋” [ํ˜‘์ƒ/์ „์†ก/์ฟ ํ‚ค]

๐Ÿ‘‰ HTTP ํ—ค๋”์˜ ์šฉ๋„ HTTP ์ „์†ก์— ํ•„์š”ํ•œ ๋ชจ๋“  ๋ถ€๊ฐ€์ •๋ณด ํ•„์š”์‹œ ์ž„์˜์˜ ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. Ex) ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์˜ ๋‚ด์šฉ, ๋ฉ”์‹œ์ง€ ๋ฐ”๋Œ€์˜ ํฌ๊ธฐ, ์••์ถ•, ์ธ์ฆ, ์„œ๋ฒ„ ์ •๋ณด ๋“ฑ๋“ฑ์ด ํฌํ•จ๋˜์–ด์žˆ๋‹ค. ๐Ÿ‘‰ HTTP์˜

dahliachoi.tistory.com

 

comsumes๋Š” ์†Œ๋น„ํ•˜๋Š” ์ž…์žฅ์ด๋‹ค. ์ƒ์‚ฐ์€ produce!

ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ์‹ค์Šต์„ ํ•ด๋ณด์•˜๊ธฐ ๋•Œ๋ฌธ์— Accept๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์‹ค์Šตํ•ด๋ณด์•˜๋‹ค.

Accept์—๋Š” ์–ด๋–ค ๋ฏธ๋””์–ด ํƒ€์ž…์„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋Š”์ง€ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

๐Ÿ‘‰ consume ์˜ˆ์‹œ

@PostMapping(value = "/mapping-consume", consumes = MediaType.APPLICATION_JSON_VALUE)
// = (consumes = "application/json")
    public String mappingConsumes() {
        log.info("mappingConsumes");
        return "ok";
    }

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” Content-Type์ด application/josn์ผ ๋•Œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ’ป POSTMAN์œผ๋กœ ์‹ค์Šตํ•ด๋ณด์ž!

 

 

โ—พ Content-Type์„ text๋กœ ํ•ด๋ดค์„ ๋•Œ

์ด๋ ‡๊ฒŒ HTTP 415 ์ƒํƒœ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค!

 

 

โ—พ ์•Œ๋งž์€ json ํ˜•ํƒœ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ

์•Œ๋งž๊ฒŒ ๋ฐ˜ํ™˜๋์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 

๐Ÿ‘‰ produce ์˜ˆ์‹œ

 

@PostMapping(value = "/mapping-produce", produces = MediaType.TEXT_HTML_VALUE) 
// = (produces = "text/html")
    public String mappingProduces() {
        log.info("mappingProduces");
        return "ok";
    }

์œ„์ฒ˜๋Ÿผ produces๋Š” text/html๋กœ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด, html๋งŒ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ด๊ฒƒ๋„ ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ํ™•์ธํ•ด๋ณด์ž!

ํฌ์ŠคํŠธ๋งจ์˜ header์— ์žˆ๋Š” Accept๋กœ ๋ฏธ๋””์–ด ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

โ—พ Accept๋ฅผ json (or ๋‹ค๋ฅธ ํƒ€์ž…)์œผ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ

 

์ด๋ ‡๊ฒŒ HTTP 406 ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค!! application/json์œผ๋กœ ํ•˜๋ฉด jsonํ˜•ํƒœ๋งŒ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

โ—พ text/html๋กœ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ( */*๋กœ ํ•ด๋„ ์ž˜ ์ž‘๋™๋จ)

 

 

์ž˜ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 


์ด ๊ฐœ๋…์„ ๋ฐฐ์šฐ๊ณ  ํšŒ์›๊ด€๋ฆฌ api์— ์ ์šฉ๋งŒ ํ•ด๋ณด์•˜๋Š”๋ฐ, ํ™•์‹คํžˆ ์ฝ”๋“œ๋„ ์งง์•„์ง€๊ณ  ๊ฐ™์€ url์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์„ ๋ณ€ํ™˜์‹œ์ผœ์„œ ์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. 

 

์ด ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ [์Šคํ”„๋งMVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ ]์„ ๋“ฃ๊ณ  ์ž‘์„ฑ๋œ ๋ณต์Šต ์ •๋ฆฌ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.