Back-End ์ž‘์—…์‹ค/Spring Framework

[Spring] @Controller์™€ @RestController์˜ ์ฐจ์ด

์ฃผ๋‹ˆ์“ฐ๐Ÿง‘‍๐Ÿ’ป 2022. 3. 22. 20:58
728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿš€ ๊ฐœ์š”

Spring์—๋Š” Controller๋ฅผ ์ง€์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ๋‘ ๊ฐ€์ง€ Annotaion์ด ์ค€๋น„ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.
์ฒซ๋ฒˆ์งธ๋Š” `@Controller`๊ฐ€ ์žˆ๊ณ , ๋‘๋ฒˆ์งธ๋Š” `@RestController`๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

๋‘ ์นœ๊ตฌ์˜ ์ฃผ์š” ์ฐจ์ด์ ์€ `HTTP Response Body`๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฐฉ์‹์— ์žˆ๋‹ต๋‹ˆ๋‹ค.

 

 

๐Ÿš€ @Controller - Spring MVC Controller

    ๐Ÿ”ฝ  Controller - View

์ „ํ†ต์ ์ธ Spring MVC Controoler์ธ `@Controller`๋Š” ์ฃผ๋กœ View ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์—์š”. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด Spring MVC Controller๋Š” Client์˜ ์š”์ฒญ์—์„œ View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

โ— Client(Front End)๋Š” URI ํ˜•์‹์œผ๋กœ Server (Back End)์— Request(์š”์ฒญ)์„ ๋‚ ๋ฆฐ๋‹ค.
โ— Mapping ๋˜๋Š” Handler์™€ ํ•ด๋‹น Type์„ ์ฐพ๋Š” `DispatcherServlet`์ด ํ•ด๋‹น ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค(Intercept).
โ— Controller๊ฐ€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ๋’ค Response(์‘๋‹ต)์„ `DispatcherServlet`์—๊ฒŒ ๋ฐ˜ํ™˜ํ•˜๊ณ , `DispatcherServlet`์€ View๋ฅผ ์ด์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

`@Controller`๊ฐ€ `View`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„  `ViewResolver`๊ฐ€ ์ด์šฉ๋˜๊ณ , `ViewResolver`์„ค์ •์— ๋งž๊ฒŒ `View`๋ฅผ ์ฐพ์•„ Renderingํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

 

    ๐Ÿ”ฝ  Controller - Data

Spring MVC์˜ Controller์—์„œ๋„ `Data ๋ฐ˜ํ™˜`ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด์—์š”.
Spring MVC์˜ Controller์—์„œ๋Š” Data๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด @ResponseBody
Annotaion์„ ์ด์šฉํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”. Controller์—์„œ๋„ JSON ํ˜•ํƒœ๋กœ Data๋ฅผ ์ „๋‹ฌ ํ•ด ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค.

 

 

โ— Client(Front End)๋Š” URI ํ˜•์‹์œผ๋กœ Server (Back End)์— ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
โ— Mapping๋˜๋Š” Handler์™€ ํ•ด๋‹น Type์„ ์ฐพ๋Š” `DispatcherServlet`์ด ํ•ด๋‹น ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค(Intercept).
โ— `@ResponseBody`๋ฅผ ์ด์šฉํ•˜์—ฌ Client์—๊ฒŒ JSON ํ˜•ํƒœ๋กœ Data๋ฅผ ์ „๋‹ฌํ•ด ์ค€๋‹ค.

 

`@RestController`๊ฐ€ Data๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” `viewResolver` ๋Œ€์‹  `HttpMessageConverter`๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ด์—์š”. HttpMessageConverter์—๋Š” ์—ฌ๋Ÿฌ Converter(๋ณ€ํ™˜๊ธฐ)๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”๋ฐ, ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” Data์— ๋”ฐ๋ผ ์‚ฌ์šฉ๋˜๋Š” Converter๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์ด์—์š”. ๋งŒ์•ฝ ๋‹จ์ˆœ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด `StringHttpMessageConverter`๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๊ณ , ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ์—๋Š” `MappingJackson2HttpMessageConverter`๊ฐ€ ๋™์ž‘ํ•˜๋ฉฐ, Data ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ MessageConverter๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

Spring์€ Client์˜ `HTTP Accept Header`์™€ Server Controller ๋ฐ˜ํ™˜ Type ์ •๋ณด ๋‘ ๊ฐ€์ง€๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ ‘ํ•ฉํ•œ HttpMessageConverter๋ฅผ ์„ ํƒํ•˜์—ฌ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

์˜ˆ์ œ ์ฝ”๋“œ

๋ฐ˜์‘ํ˜•

 

์œ„์™€ ๊ฐ™์€ ์˜ˆ์ œ์—์„œ info๋ผ๋Š” Data๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ์ž ํ•˜๊ณ  ์žˆ๊ณ , User๋ฅผ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด @ResponseBody๋ผ๋Š” Annotation์„ ๋ถ™์—ฌ์ฃผ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”. infoView Method์—์„œ๋Š” View๋ฅผ ์ „๋‹จํ•ด์ฃผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— String์„ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด ์ค€ ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค. (Data๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” RestController์™€ View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Controller๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.)

 

 

 

 

๐Ÿš€ @Controller - Spring RestFul Controller

`@RestController`๋Š” Spring MVC Controller์— `@ResponseBody`๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ RestController์˜ ์ฃผ ์šฉ๋„๋Š” `JSON ํ˜•ํƒœ์˜ ๊ฐ์ฒด๋กœ Data ๋ฐ˜ํ™˜ ๋ฐ ์ „๋‹ฌ`ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ตœ๊ทผ Data๋ฅผ ์‘๋‹ต์œผ๋กœ ์ œ๊ณตํ•˜๋Š” RestFul API๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ์ฃผ๋กœ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค.

 

โ— Client(Front End)๋Š” URI ํ˜•์‹์œผ๋กœ Server (Back End)์— ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
โ— Mapping๋˜๋Š” Handler์™€ ํ•ด๋‹น Type์„ ์ฐพ๋Š” `DispatcherServlet`์ด ํ•ด๋‹น ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค(Intercept).
โ— `RestController`๋Š” ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ , Data๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

์˜ˆ์ œ ์ฝ”๋“œ

info1์˜ Method๋Š” User Data๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” info2์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ Data์™€ HTTP Status Code๋ฅผ ํ•จ๊ป˜ ์ œ์–ด ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ๊ฒƒ์ด์—์š”. ๋˜ํ•œ, ๋งŒ์•ฝ userService์—์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ `Optional`์ด๋ผ๋ฉด info3์ฒ˜๋Ÿผ ๊น”๋”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•