[ @Controller์™€ @RestController ]

 

Spring - @Controller์™€ @RestController ๊ทธ๋ฆฌ๊ณ  ์ƒํƒœ์ฝ”๋“œ

[ SpringMVC์— ๋Œ€ํ•˜์—ฌ ] Spring - Spring MVC๋ฅผ ์•Œ์•„์•ผ ์‹œ์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.์ฒ˜์Œ์—” ๋Œ€์ถฉ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๋ คํ–ˆ์ง€๋งŒ,์ด๋Ÿด์ˆ˜๊ฐ€! ์•„๋Š” ๊ฒƒ์ด ๋ฏธ์•ฝํ•˜๋‹ˆ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์œผ๋กœ ์•Œ์•„์•ผํ•˜๋Š”

post-this.tistory.com

 

 

 

 

[ Hello,World ์ถœ๋ ฅํ•˜๊ธฐ ]

 

Spring - [IntelliJ] Hello World ์ถœ๋ ฅํ•˜๊ธฐ

[ Spring MVC์— ๋Œ€ํ•˜์—ฌ ] Spring - Spring MVC๋ฅผ ์•Œ์•„์•ผ ์‹œ์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.์ฒ˜์Œ์—” ๋Œ€์ถฉ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๋ คํ–ˆ์ง€๋งŒ,์ด๋Ÿด์ˆ˜๊ฐ€! ์•„๋Š” ๊ฒƒ์ด ๋ฏธ์•ฝํ•˜๋‹ˆ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์œผ๋กœ ์•Œ์•„์•ผํ•˜๋Š”

post-this.tistory.com

 


 

 

Controller ์•ˆ์—
@GetMapping( )์ด๋ผ๋Š” ์กด์žฌ์˜€๋‹ค.


๊ณผ์—ฐ @GetMapping์€ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ผ๊นŒ?

 

 

 

 

๋ฐ์ดํ„ฐ์—๋Š” ๋‘๊ฐ€์ง€ ์ „์†ก๋ฐฉ์‹์ด ์กด์žฌํ•œ๋‹ค.

 

 

GET

  • URL(์ฃผ์†Œ)์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ์‹œ์ผœ ๋ณด๋‚ธ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋Š” ํ—ค๋”์— ํฌํ•จ๋˜์–ด์žˆ๋‹ค.
  • URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ๋˜์–ด ๋ณด์•ˆ์— ์ทจ์•ฝํ•˜๋‹ค.
  • ์บ์‹ฑ(๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊นŒ์šด ๊ณณ์— ์ž„์‹œ๋กœ ์ €์žฅํ•œ ํ›„, ๋น ๋ฅด๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ”„๋กœ์„ธ์Šค)ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฃผ๋กœ ์กฐํšŒ, ๊ฒ€์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

POST

  • URL(์ฃผ์†Œ)์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๋””์— ํฌํ•จํ•œ๋‹ค.
  • URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ๋˜์–ด์žˆ์ง€ ์•Š์•„ GET๋ฐฉ์‹ ๋ณด๋‹จ ๋ณด์•ˆ์ด ๋†’๋‹ค.
  • ์บ์‹ฑ((๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊นŒ์šด ๊ณณ์— ์ž„์‹œ๋กœ ์ €์žฅํ•œ ํ›„, ๋น ๋ฅด๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ”„๋กœ์„ธ์Šค)ํ•  ์ˆ˜ ์—†๋‹ค.
  • ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ €์žฅ, ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 


 

 

 

 

@GetMapping( )

  • Get ์š”์ฒญ์„ ํ•˜๋Š” API ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
  • ๊ฒฝ๋กœ ๋งคํ•‘์— ๋”ฐ๋ผ ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
    (๋งคํ•‘, ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ณผ์ • ํ˜น์€ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งํ•œ๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๋ฉด, ๊ฐ•๋‚จ ๋ง›์ง‘์„ ๊ฒ€์ƒ‰ํ–ˆ์„์‹œ ๊ฐ•๋‚จ์— ์žˆ๋Š” ๋ชจ๋“  ์‹๋‹น์ด ๊ฒ€์ƒ‰๋˜๋Š” ๊ฒฝ์šฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ๋งค์นญ์€ ํ‚ค = 180cm ์ผ๋Œ€์ผ๋กœ ๋งž๋Š” ๊ฒฝ์šฐ)
  • ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฐ˜ํ™˜๊ฐ’์€ ํ•˜๋‚˜์ง€๋งŒ ๊ฒฝ๋กœ๋Š” ๋‹ค์ค‘์œผ๋กœ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•ด๋ณด์ž.

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ํŽ˜์ด์ง€ ๋ฐ˜ํ™˜์€ ์•„๋‹ˆ๊ณ  @RestController(์ง๋ ฌํ™”๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์ด ํŽ˜์ด์ง€์— ๋ณด์ด๊ฒŒ ํ•ด์ฃผ๋Š”)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ์˜ ์ผ๋ถ€์ด๋‹ค.

Controller ๋‚ด๋ถ€ ์ฝ”๋“œ

@GetMapping("")
    public String helloWorld() {
        return "Hello, World";
	}

 

  1. @GetMapping("")์œผ๋กœ ("")์— ๊ฒฝ๋กœ๊ฐ€ ์ ํ˜€์žˆ์ง€ ์•Š์•„ ๋ฃจํŠธ๋ฅผ(๋ฉ”์ธ ํŽ˜์ด์ง€) ์˜๋ฏธํ•œ๋‹ค.
  2. @GetMapping("") = ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”์ธ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€๋ฉด
  3. "Hello, World"๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ. (@RestController๋ฅผ ์ด์šฉํ•˜์—ฌ)

 

 

 

๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—” ํŽ˜์ด์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ ์ผ๋ถ€๋ฅผ ์‚ดํŽด๋ณด์ž.

 

Controller ๋‚ด๋ถ€ ์ฝ”๋“œ

@GetMapping("")
public String homePage() {
    return "First";
}

 

  1. page๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ .html ํ˜น์€ .jsp๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.
  2. ๋ฃจํŠธ ๊ฒฝ๋กœ("")๋กœ ๋“ค์–ด๊ฐ„๋‹ค๋ฉด
  3. First๋ผ๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š” ๋ช…๋ น์ด DispatcherServlet์—๊ฒŒ ์ „๋‹ฌ๋œ๋‹ค.
  4. DispatcherServlet์€ ViewResolver๋กœ ์ „๋‹ฌํ•˜๊ณ 
  5. ViewResolver๊ฐ€ First๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŽ˜์ด์ง€๋ฅผ ์ฐพ์•„ ์ตœ์ข…์ ์œผ๋กœ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋„๋ก ํ•œ๋‹ค.

 

 


 

 

 

 

@RequestMapping

  • ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ์ง€์ •๊ฐ€๋Šฅํ•˜๋‹ค.
  • value, method, params, headers, consumes, produces์˜ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ๊ทธ ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ Get, Post ๋“ฑ ๋‹ค์–‘ํ•œ ๋™์ž‘์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์†์„ฑ์ด ํ•˜๋‚˜๋ผ๋„ ๋‹ฌ๋ผ์•ผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ Handler๊ฐ€ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด @RequestMapping์—์„œ ์กฐํšŒํ•˜๋Š” ๋ชฉ์ ์— ๋งž๊ฒŒ ํŽธ๋ฆฌํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋‚ธ ๋ฒ„์ „์„ @GetMapping ๋“ฑ.. ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

 

์ด๊ฒƒ๋„ ์‰ฝ๊ฒŒ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด์ž.

@RequestMapping(value = "", method = RequestMethod.GET)
public String helloWorld() {
    return "Hello,Wrold!";
}

=

@GetMapping("")
    public String helloWorld() {
        return "Hello, World";
    }

 

@RequestMapping์€ method์— .GET์œผ๋กœ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ• ๊ฑด์ง€ ๋ช…์‹œํ•˜๊ณ  ์žˆ๋‹ค. 

 

๋” ์ž์„ธํ•œ ๊ฑด ๋‚˜์ค‘์— ์ž‘์„ฑํ•˜๊ฒ ๋‹ค.

 

 

 


 

 

 

 

๋ชฉ์ ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์–ด๋…ธํ…Œ์ด์…˜

  • @PostMapping =  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒŒ์‹œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. (Post์˜ ํŠน์ง•์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ ๊ฒƒ)
  • @PutMapping = ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ ์ „์ฒด๋ฅผ ๊ฐฑ์‹ ํ•˜๋‹ค.
  • @DeleteMapping = ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • @PatchMapping = ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ˆ˜์ •๋œ ๋ถ€๋ถ„๋งŒ ๊ฐฑ์‹ ํ•œ๋‹ค.

 

@GetMapping์„ ํฌํ•จํ•˜์—ฌ ์œ„์˜ ์–ด๋…ธํ…Œ์ด์…˜๋“ค์€ @RequestMapping์—์„œ ๋ชฉ์ ์— ๋งž๊ฒŒ ๋” ํŽธ๋ฆฌํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. @RequestMapping์—์„œ ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ์กฐํ•ฉํ•ด ์›ํ•˜๋Š” ์ž‘๋™๋ฐฉ์‹์„ ๋งŒ๋“ค์–ด์•ผํ–ˆ๋‹ค๋ฉด, ์œ„์˜ ์–ด๋…ธํ…Œ์ด์…˜์€ ์†์„ฑ์˜ ์กฐํ•ฉ์— ๋Œ€ํ•œ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ค€ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.


 

 

@GetMapping์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ ํ•œ ์ค„๋กœ ์ •๋ฆฌํ•˜๋ฉด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ๋กœ์— ๋งž์ถฐ ๋ฐ˜ํ™˜๋  ํŽ˜์ด์ง€๋ฅผ ๊ณจ๋ผ์ฃผ๋Š” ์—ญํ• ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”.
@RequestMapping์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ์— ์ •๋ฆฌํ•ด๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค.

 

 

์•„๋ž˜์— ์ถœ์ฒ˜๋ฅผ ๋‚จ๊ฒจ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค.
๋ธ”๋กœ๊ทธ์—” ์ œ๊ฐ€ ์ตœ๋Œ€ํ•œ ์ดํ•ดํ•œ ๋‚ด์šฉ๊นŒ์ง€๋งŒ ์ ๋Š”๊ฑฐ๋ผ
์ถœ์ฒ˜์— ๋“ค์–ด๊ฐ€์‹œ๋ฉด ๋” ๋งŽ์€ ๋‚ด์šฉ์ด ์žˆ์–ด ๊ณต๋ถ€ํ•˜์‹œ๋Š”๋ฐ ๋„์›€๋˜์‹ค๊ฒ๋‹ˆ๋‹ค.

 

๐Ÿชฝ

ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”

 

 


 

 

 

- @GetMapping์˜ ๊ฐœ๋…

 

GetMapping ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋‹ค์ค‘๋งตํ•‘ํ•˜๊ธฐ

@GetMapping ์–ด๋…ธํ…Œ์ด์…˜์€ HTTP GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งตํ•‘(@RequestMapping) ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ๋ฉ”์„œ๋“œ(url)์— ๋”ฐ๋ผ ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ๋Š”

youngjinmo.github.io

 

 

 

- Get๊ณผ Post

 

Controller์˜ PostMapping, GetMapping ์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ธฐ

ใ…‡ GET/POST ์ดํ•ดํ•˜๊ธฐ 1) GET ๋ž€? - URL์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ์‹œ์ผœ ์š”์ฒญ - ๋ฐ์ดํ„ฐ๋ฅผ ํ—ค๋”์— ํฌํ•จํ•˜์—ฌ ์ „์†ก - URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ๋˜์–ด ๋ณด์•ˆ์— ์ทจ์•ฝ - ์บ์‹ฑํ•  ์ˆ˜ ์žˆ์Œ => ์ฃผ๋กœ ์กฐํšŒํ• ๋•Œ๋งŒ ์‚ฌ์šฉ 2) POST ๋ž€? - URL์—

tjdwns4537.tistory.com

 

 

 

- @RequestMapping ๋™์ž‘๋ฐฉ์‹

 

[Spring] @RequestMapping์ด๋ž€ ๊ทธ๋ฆฌ๊ณ  ๋™์ž‘ ๋ฐฉ์‹

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Spring ๊ฐœ๋ฐœ ์‹œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” @RequestMapping์˜ ๊ฐœ๋… ๋ฐ ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด ์ •๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. [๋ชฉ์ฐจ] 1. @RequestMapping์ด๋ž€? 2. @RequestMapping ์ž‘์„ฑ ์˜ˆ์‹œ 3. @RequestMapping ๋™์ž‘ ๋ฐฉ์‹ 1. @RequestMapping

backendcode.tistory.com

 

 

 

- @RequestMapping ์˜ต์…˜

 

[Spring] @RequestMapping ์˜ต์…˜

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜์˜ ์˜ต์…˜๋“ค์— ๋Œ€ํ•ด ์ •๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ตญ๋น„ ๊ณผ์ •์„ ๋“ค์œผ๋ฉฐ, ๊ธฐ์กด์— ์•Œ๊ณ  ์žˆ๋˜ @RequestMapping ์˜ต์…˜๋“ค๊ณผ ๋‹ค๋ฅธ ์˜ต์…˜๋“ค์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์–ด ๊ธฐ์กด์— ์•Œ๊ณ  ์žˆ

backendcode.tistory.com

 

 

 

- ๋งคํ•‘๊ณผ ๋งค์นญ

 

#002.๋งคํ•‘๊ณผ ๋งค์นญ์˜ ์ฐจ์ด, ์•Œ๊ณ  ๊ณ„์„ธ์š”?

์ฃผ๋‹ˆ์–ด ๊ธฐํš์ž ์‹œ์ ˆ, ๊ฐœ๋ฐœ์ž๋“ค๊ณผ ์ผํ•˜๋ฉด์„œ ๋งค์นญ(Matching)์ด๋ž€ ๋‹จ์–ด์™€ ๋งคํ•‘(Mapping)์ด๋ž€ ๋‹จ์–ด๋ฅผ ์ž์ฃผ ๋“ค์—ˆ์—ˆ๋Š”๋ฐ, ๋‹น์‹œ๋งŒ ํ•ด๋„ ์ด ๋‘ ๋‹จ์–ด์˜ ์ฐจ์ด๋ฅผ ์ž˜ ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ๊ฐœ๋ฐœ์ž ๋งˆ๋‹ค ์–ด๋–ค ๊ฐœ๋ฐœ์ž๋Š”

yamestyle.com

 

 

+ Recent posts