๐ŸŒˆ [Section3] 1. [ Spring MVC ] API ๊ณ„์ธต 1

ํ˜„์ฃผยท2022๋…„ 10์›” 20์ผ
0

bootcamp

๋ชฉ๋ก ๋ณด๊ธฐ
41/71

๐Ÿ“• ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ!

  • Spring MVC
  • Spring MVC ์š”์ฒญ ์ฒ˜๋ฆฌ ํ๋ฆ„
  • Spring MVC๋ฅผ ์ด์šฉํ•œ ์‹ค์Šต

โœ๏ธ Spring MVC

  • ์„œ๋ธ”๋ฆฟ(Servlet) API๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

  • Spring Web MVC ๋ฅผ ์ค„์—ฌ์„œ Spring MVC
    ( Spring MVC ํ”„๋ ˆ์ž„์›Œํฌ ๋ผ๊ณ ๋„ ๋ถ€๋ฆ„ )

โœ”๏ธ ์„œ๋ธ”๋ฆฟ(Servlet)
โžœ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํŠน์ • ๊ทœ์•ฝ์— ๋งž์ถ”์–ด์„œ Java ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋Š” ํด๋ž˜์Šค ํŒŒ์ผ
โ €
( ์•„ํŒŒ์น˜ ํ†ฐ์บฃ(Apache Tomcat) - ์„œ๋ธ”๋ฆฟ๋“ค์ด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ์‹คํ–‰์ด ๋˜๋„๋ก ํ•ด์ฃผ๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ(Servlet Container) ์ค‘ ํ•˜๋‚˜ )

โœ” M = Model

โžœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์œผ๋กœ ๋Œ๋ ค์ค„ ์ž‘์—…์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ

  • ์„œ๋น„์Šค ๊ณ„์ธต (Service Layer)
    โžœ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์‚ฌํ•ญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜์—ญ
    โ €
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง (Business Logic)
    โžœ ์‹ค์ œ๋กœ ์š”์ฒญ ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Java ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ

โœ” V = View

โžœ Model ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ์›น๋ธŒ๋ผ์šฐ์ € ๊ฐ™์€ ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” ๋ฆฌ์†Œ์Šค(Resource)๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• 

โœ”๏ธ View์˜ ํ˜•ํƒœ
1. HTML ํŽ˜์ด์ง€์˜ ์ถœ๋ ฅ
โžœ HTML ํŽ˜์ด์ง€๋ฅผ ์ง์ ‘ ๋ Œ๋”๋งํ•ด์„œ ํด๋ผ์ด์–ธํŠธ ์ธก์— ์ „์†กํ•˜๋Š” ๋ฐฉ์‹
( SSR์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Œ )
โžœ HTML ํƒœ๊ทธ๋กœ ๊ตฌ์„ฑ๋œ ํŽ˜์ด์ง€์— Model ๋ฐ์ดํ„ฐ๋ฅผ ์ฑ„์›Œ ๋„ฃ์€ ํ›„, ์ตœ์ข…์ ์ธ HTML ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ํด๋ผ์ด์–ธํŠธ ์ธก์— ์ „์†กํ•ด์คŒ
( But, ์ด๋Š” ํ”„๋ก ํŠธ์—”๋“œ, ๋ฐฑ์—”๋“œ๊ฐ€ ํ†ตํ•ฉ๋œ ๊ตฌ์กฐ )
โ €
2.PDF, Excel ๋“ฑ์˜ ๋ฌธ์„œ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ
โžœ Model ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ PDF ๋ฌธ์„œ๋‚˜ Excel ๋ฌธ์„œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํด๋ผ์ด์–ธํŠธ ์ธก์— ์ „์†กํ•˜๋Š” ๋ฐฉ์‹
โžœ ๋ฌธ์„œ ๋‚ด์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹
โ €
3. XML, JSON ๋“ฑ ํŠน์ • ํ˜•์‹์˜ ํฌ๋งท์œผ๋กœ์˜ ๋ณ€ํ™˜
โžœ Model ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ํ”„๋กœํ† ์ฝœ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ณ€ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ ์ธก์— ์ „์†กํ•˜๋Š” ๋ฐฉ์‹
โžœ ํŠน์ • ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ „์†กํ•˜๊ณ , ํ”„๋ŸฐํŠธ์—”๋“œ ์ธก์—์„œ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ HTML ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹
( CSR์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Œ)

  • ์žฅ์ 
    • ํ”„๋ŸฐํŠธ์—”๋“œ ์˜์—ญ / ๋ฐฑ์—”๋“œ ์˜์—ญ์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ โžœ ๊ฐœ๋ฐœ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ์šฉ์ด
    • ํ”„๋ŸฐํŠธ์—”๋“œ ์ธก์—์„œ ๋น„๋™๊ธฐ ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง

โœ”๏ธ JSON ( JavaScript Object Notation )
โžœ Spring MVC์—์„œ ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์„œ๋ฒ„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ ํ˜•์‹
โžœ ๊ธฐ๋ณธ ํฌ๋งท = {โ€์†์„ฑโ€:โ€๊ฐ’โ€} ํ˜•ํƒœ

โœ” C = Contoller

  • ํด๋ผ์ด์–ธํŠธ ์ธก์˜ ์š”์ฒญ์„ ์ง์ ‘์ ์œผ๋กœ ์ „๋‹ฌ ๋ฐ›๋Š” ์—”๋“œํฌ์ธํŠธ(Endpoint)

  • Model๊ณผ View์˜ ์ค‘๊ฐ„์—์„œ ์ƒํ˜ธ ์ž‘์šฉ์„ ํ•ด์ฃผ๋Š” ์—ญํ• 
    ( ์š”์ฒญ ์ „๋‹ฌ๋ฐ›๊ณ , Model ๋ฐ์ดํ„ฐ๋ฅผ View๋กœ ์ „๋‹ฌ )

โœ”๏ธ ์—”๋“œํฌ์ธํŠธ (Endpoint)
์ปดํ“จํ„ฐ์™€ ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐํ•˜๊ณ  ์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ์™€ ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•˜๋Š” ๋ฌผ๋ฆฌ์  ๋””๋ฐ”์ด์Šค

โœ” Spring MVC ์ตœ์ข… ํ๋ฆ„

( ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถ”๋ฆฐ ๋™์ž‘ ๋ฐฉ์‹ )

  1. Client๊ฐ€ ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ „์†ก
  2. Controller๊ฐ€ ๋ฐ›์•„์„œ ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
  3. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ
  4. Model ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
  5. Controller์—๊ฒŒ Model ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
  6. Controller๊ฐ€ View์—๊ฒŒ Model ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
  7. View๊ฐ€ ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

โœ๏ธ JASON ํ‘œ๊ธฐ๋ฒ• ์‚ฌ์šฉ ์—ฐ์Šต

[์ฐธ๊ณ ] https://jsonformatter.org/json-to-java ์ด์šฉ

์œ„์™€ ๊ฐ™์ด ์ง์›๋“ค์˜ ์ •๋ณด๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋„ฃ๊ณ  ๋Œ๋ ค๋ณด๋‹ˆ,
์•„๋ž˜์™€ ๊ฐ™์ด Java๋กœ ๋ณ€๊ฒฝ๋˜์–ด ์ž˜ ๋‚˜์™”๋‹ค!

[์ฐธ๊ณ ] https://codebeautify.org/blog/json-full-form/


โœ๏ธ Spring MVC ์š”์ฒญ ์ฒ˜๋ฆฌ ํ๋ฆ„

(1) ๋จผ์ € ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ์ „์†กํ•˜๋ฉด DispatcherServlet ํด๋ž˜์Šค์— ์š”์ฒญ์ด ์ „๋‹ฌ๋จ

๐Ÿ”Š ํด๋ผ์ด์–ธํŠธ๊ฐ€ DispatcherServletํ•œํ…Œ ์š”์ฒญ ์ „์†กํ•˜๊ณ  ์ „๋‹ฌ ๋ฐ›์Œ

(2) DispatcherServlet ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Controller์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰์„ HandlerMapping ์ธํ„ฐํŽ˜์ด์Šค์—๊ฒŒ ์š”์ฒญ

๐Ÿ”Š DispatcherServlet โžœ HandlerMapping "์•ผ Contoller ์ข€ ๊ฒ€์ƒ‰ํ•ด๋ด"

(3) HandlerMapping์ด ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ + ๋งคํ•‘๋˜๋Š” ํ•ธ๋“ค๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ DispatcherServlet์—๊ฒŒ ๋ฆฌํ„ด

๐Ÿ”Š HandlerMapping โžœ DispatcherServlet "์•ผ ๊ทธ ์š”์ฒญ์— ์•Œ๋งž์€ Controller ์ฐพ์•˜์–ด"
( ์—ฌ๊ธฐ์„œ ํ•ธ๋“ค๋Ÿฌ ๊ฐ์ฒด - Controller ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ)

  • ํ•ธ๋“ค๋Ÿฌ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ํ•ธ๋“ค๋Ÿฌ์˜ Handler ๋ฉ”์„œ๋“œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ
    ( Handler ๋ฉ”์„œ๋“œ = Controller ํด๋ž˜์Šค ์•ˆ์— ๊ตฌํ˜„๋œ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ )

(4) ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Controller ํด๋ž˜์Šค๋ฅผ ์ฐพ์•˜์œผ๋‹ˆ, ์ด์ œ ์‹ค์ œ๋กœ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Handler ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์•„ ํ˜ธ์ถœํ•ด์•ผ ํ•จ
DispatcherServlet์ด Handler ๋ฉ”์„œ๋“œ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , HandlerAdpater์—๊ฒŒ Handler ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์œ„์ž„ํ•จ

๐Ÿ”Š DispatcherServlet โžœ HandlerAdpater "์•ผ ์ด๊ฑฐ Handler ํ•œํ…Œ ์ฒ˜๋ฆฌํ•˜๋ผ๊ณ  ํ•ด"

(5) HandlerAdapter๊ฐ€ DispatcherServlet์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ Controller ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ, ํ•ด๋‹น Controller์˜ Handler ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

๐Ÿ”Š HandlerAdapter โžœ Controller "์•ผ Handler ๋ฉ”์„œ๋“œ ๋„ˆ ๋‚˜์™€์„œ ์ด๊ฑฐ ์ฒ˜๋ฆฌํ•ด๋ด"
( ์ด Controller ํด๋ž˜์Šค๊ฐ€ Spring MVC์—์„œ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์˜ ์ตœ์ข… ๋ชฉ์ ์ง€ )

์—ฌ๊ธฐ๊นŒ์ง€ ํ๋ฆ„์˜ ๋ฐ˜์„ ๋Ž ! ์ด์ œ ๋ฐ˜๋Œ€๋กœ ~

(6) Controller์˜ Handler ๋ฉ”์„œ๋“œ๊ฐ€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ ํ›„ ๋ฆฌํ„ด ๋ฐ›์€ ๊ฒฐ๊ณผ๋ฌผ์ธ Model ๋ฐ์ดํ„ฐ๋ฅผ HandlerAdapter์—๊ฒŒ ์ „๋‹ฌ

๐Ÿ”Š Controller โžœ HandlerAdapter "์•ผ ์ด๊ฑฐ ๋‹ค ํ•ด์„œ ๊ฒฐ๊ณผ๋ฌผ(Model) ๋„ˆํ•œํ…Œ ์ค„๊ฒŒ"

(7) HandlerAdapter๊ฐ€ ์ „๋‹ฌ๋ฐ›์€ Model ๋ฐ์ดํ„ฐ์™€ View ์ •๋ณด๋ฅผ ๋‹ค์‹œ DispatcherServlet์—๊ฒŒ ์ „๋‹ฌ

๐Ÿ”Š HandlerAdapter โžœ DispatcherServlet "์•ผ ๊ฒฐ๊ณผ๋ฌผ(Model)์ด๋ž‘ View ์ •๋ณด ์ค„๊ฒŒ"

(8) DispatcherServlet์ด ์ „๋‹ฌ ๋ฐ›์€ View ์ •๋ณด๋ฅผ ๋‹ค์‹œ ViewResolver์—๊ฒŒ ์ „๋‹ฌํ•ด์„œ View ๊ฒ€์ƒ‰ ์š”์ฒญ

๐Ÿ”Š DispatcherServlet โžœ ViewResolver "์•ผ ์ด View ์ •๋ณด ๊ฒ€์ƒ‰ํ•ด๋ด"

(9) ViewResolver๊ฐ€ View ์ •๋ณด์— ํ•ด๋‹นํ•˜๋Š” View๋ฅผ ์ฐพ์•„์„œ View๋ฅผ ๋‹ค์‹œ ๋ฆฌํ„ด

๐Ÿ”Š ViewResolver โžœ DispatcherServlet "๊ทธ ์ •๋ณด์— ํ•ด๋‹นํ•˜๋Š” View ์—ฌ๊ธฐ ์žˆ์–ด"

(10) DispatcherServlet์ด ViewResolver๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ View ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด Model ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ, ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•  ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์„ ์š”์ฒญ

๐Ÿ”Š DispatcherServlet โžœ View ๊ฐ์ฒด "์•ผ ์—ฌ๊ธฐ ๊ฒฐ๊ณผ๋ฌผ(Model) ์žˆ์œผ๋‹ˆ๊นŒ ํด๋ผ์ด์–ธํŠธํ•œํ…Œ ์ค„ ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ข€ ๋งŒ๋“ค์–ด๋ด"

(11) View๊ฐ€ ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ•ด์„œ ๋‹ค์‹œ DispatcherServlet์—๊ฒŒ ์ „๋‹ฌ

๐Ÿ”Š View โžœ DispatcherServlet "์•ผ ์—ฌ๊ธฐ ๋‹ค ๋งŒ๋“ฆ"

(12) DispatcherServlet์ด View๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์ข…์ ์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ

๐Ÿ”Š DispatcherServlet โžœ ํด๋ผ์ด์–ธํŠธ "์ž ๋‹ค ํ–ˆ์–ด. ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ค„๊ฒŒ"

โ— ์œ„ ํ๋ฆ„์„ ๋ณด๋ฉด DispatcherServlet์ด ๊ต‰์žฅํžˆ ์ค‘๊ฐ„์—์„œ ํ•˜๋Š” ์ผ์ด ๋งŽ์•„ ๋ฐ”๋น  ๋ณด์ž„ !
But, ์‹ค์ œ๋กœ ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๋“ค์—๊ฒŒ ๋‹ค ์œ„์ž„(Delegate)ํ•˜๊ณ  ์žˆ์Œ
(Front Controller Pattern)

โžœ DispatcherServlet = ๋ฐ”์ง€์‚ฌ์žฅ

โœ” Front Controller Pattern

โžœ DispatcherServlet์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ€์žฅ ์•ž๋‹จ์— ๋ฐฐ์น˜๋˜์–ด ๋‹ค๋ฅธ ๊ตฌ์„ฑ์š”์†Œ๋“ค๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉด์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํŒจํ„ด

โœ”๏ธ Handler ๋ฉ”์„œ๋“œ
โžœ Controller ํด๋ž˜์Šค์— ์žˆ๋Š” ์• ๋„ˆํ…Œ์ด์…˜์ด ๋ถ™์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค
Ex. @GetMapping, @PostMapping, PatchMapping ๋“ฑ

โœ”๏ธ HandlerMapping
์‚ฌ์šฉ์ž์˜ ์š”์ฒญ๊ณผ ์ด ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” Handler๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š” ์—ญํ• 
โžœ HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ(GET,POST ๋“ฑ)์™€ Mapping URL์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋‹น Handler์™€ ๋งคํ•‘
( ์—ฌ๋Ÿฌ ๋ชจ๋“  ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ๋“ค์˜ @~Mapping ์• ๋„ˆํ…Œ์ด์…˜ ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ )
โ €
( Spring ์—์„œ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์œ ํ˜•์˜ HandlerMapping ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ ์‹ค๋ฌด์—์„œ๋Š” ์ฃผ๋กœ Spring์—์„œ ๋””ํดํŠธ๋กœ ์ •ํ•œ RequestMappingHandlerMapping ์‚ฌ์šฉ )

โœ”๏ธ HandelrAdpter
๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ํ•ธ๋“ค๋Ÿฌ๋ฅผ Spring MVC์— ํ†ตํ•ฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
โ €
โœ”๏ธ Adapter (์–ด๋Œ‘ํ„ฐ)
๋ฌด์–ธ๊ฐ€๋ฅผ ๋‹ค๋ฅธ ํ˜•์‹์ด๋‚˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์—ญํ• 

โœ”๏ธ Resolve
๋ฌด์–ธ๊ฐ€๋ฅผ ํ•ด์„ํ•˜๊ณ  ํ•ด๊ฒฐํ•ด์ฃผ๋‹ค


โœ”๏ธ API ๊ณ„์ธต

( ์ €๋ฒˆ์— ๋ฐฐ์› ์ง€๋งŒ ์žŠ์—ˆ์„๊นŒ๋ด ํ•œ๋ฒˆ ๋” ์ฒจ๋ถ€ํ•œ๋‹ค )


โœ๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ œ์ž‘์„ ์œ„ํ•ด ์ œ์ผ ๋จผ์ € ํ•ด์•ผ๋˜๋Š” ์ผ

  1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ

    โœ”๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค
    โžœ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž์ถฐ์„œ ์–ด๋–ค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ธฐ๋Šฅ์„ ํŠน์ • ๋ฒ”์œ„๋กœ ์ œํ•œํ•˜๋Š” ๊ฒƒ

  2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„์„ ์œ„ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ˆ˜์ง‘ํ•˜๋Š” ์ผ

  3. ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ์ƒ์„ฑ

โœ๏ธ Spring Boot ๊ธฐ๋ฐ˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Java ํŒจํ‚ค์ง€ ๊ตฌ์กฐ

1. ๊ธฐ๋Šฅ ๊ธฐ๋ฐ˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ (package-by-feature)

  • ๊ธฐ๋Šฅ ๋ณ„๋กœ ํŒจํ‚ค์ง€ ๋‚˜๋ˆ„๊ธฐ
    ( ์ด ํŒจํ‚ค์ง€ ์•ˆ์—๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ ์™„์„ฑ์„ ์œ„ํ•œ ๊ณ„์ธต๋ณ„ ํด๋ž˜์Šค๋“ค์ด ๋ชจ์—ฌ์žˆ์Œ )

    Ex. Coffee, Member ๋“ฑ
    ( Coffee ํŒจํ‚ค์ง€ ์•ˆ์—๋Š” CoffeeController, CoffeeDto, CoffeeRepository ๋“ฑ์˜ ๊ธฐ๋Šฅ์ด ์žˆ์Œ )

2. ๊ณ„์ธต ๊ธฐ๋ฐ˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ (package-by-layer)

  • ๊ณ„์ธต ๋ณ„๋กœ ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ
    ( ๊ฐ ํŒจํ‚ค์ง€ ์•ˆ์—๋Š” ๊ฐ ๊ธฐ๋Šฅ๋“ค์ด ๋ชจ์—ฌ์žˆ์Œ )

    Ex. controller, dto, model, repository, service ๋“ฑ
    ( controller ํŒจํ‚ค์ง€ ์•ˆ์—๋Š” CoffeeController, MemberController ๋“ฑ์˜ ๊ธฐ๋Šฅ์ด ์žˆ์Œ )

โ— ์œ„ ๋‘๊ฐœ๋ฅผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์•Œ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€๋งŒ,
ํ…Œ์ŠคํŠธ์™€ ๋ฆฌํŒฉํ† ๋ง์ด ์šฉ์ดํ•˜๊ณ , ํ–ฅํ›„์— ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค ์‹œ์Šคํ…œ์œผ๋กœ์˜ ๋ถ„๋ฆฌ๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ์šฉ์ดํ•œ ๊ธฐ๋Šฅ ๊ธฐ๋ฐ˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ์‚ฌ์šฉ ๊ถŒ์žฅํ•จ

โœ๏ธ Spring Boot ๊ธฐ๋ฐ˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ •์ƒ์ ์ธ ์‹คํ–‰์„ ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ๋  ์ผ

โžœ main() ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์—”ํŠธ๋ฆฌํฌ์ธํŠธ(Entrypoint) ์ž‘์„ฑ

But, Spring Initialize๋ฅผ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ์—”ํŠธ๋ฆฌํฌ์ธํŠธ ํด๋ž˜์Šค๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜์–ด ์žˆ์Œ

โœ”๏ธ ์—”ํŠธ๋ฆฌํฌ์ธํŠธ (Eentrypoint)
โžœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘์ 


โœ๏ธ Controller ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ ์• ๋„ˆํ…Œ์ด์…˜

  • @RequestParam
    โžœ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ, form-data ๋“ฑ์˜ Servlet request Parameter๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ

  • @RequestHeader
    โžœ request header๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์„œ header์˜ key/value์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ

  • @RequestBody
    โžœ request body๋ฅผ ์ฝ์–ด์„œ ์ง€์ •ํ•œ Java ๊ฐ์ฒด๋กœ deserialization ํ•ด์คŒ

  • @RequestPart
    โžœ 'multipart/form-data' ํ˜•์‹์˜ request ๋ฐ์ดํ„ฐ๋ฅผ part ๋ณ„๋กœ ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คŒ

  • @PathVariable
    โžœ @RequestMapping ์— ํŒจํ„ด ํ˜•์‹์œผ๋กœ ์ •์˜๋œ URL์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คŒ

  • @MatrixVariable
    โžœ URL ๊ฒฝ๋กœ ์„ธ๊ทธ๋จผํŠธ ๋ถ€๋ถ„์— key/value ์Œ์œผ๋กœ ๋œ ๋ฐ์ดํ„ฐ์— ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คŒ

  • HttpEntity
    โžœ request header์™€ body์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

[์ฐธ๊ณ ] https://itvillage.tistory.com/41


๐Ÿ˜œ Spring MVC๋ฅผ ์ด์šฉํ•œ ์‹ค์Šต

  • Project ํด๋”์˜ Section3-Week1 ํŒŒ์ผ์— ์žˆ์Œ

  • ์ž์„ธํ•œ ์„ค๋ช…์€ ์›Œ๋“œ ์ฐธ๊ณ  !

JSON ๋ฌธ์ž์—ด์„ ์ˆ˜์ž‘์—…ํ•˜๋Š” ๊ฒƒ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ
โžœ Map ๊ฐ์ฒด๋กœ ๋Œ€์‹ ํ•˜๊ณ  ํƒ€์ž…์„ ResponseEntity ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•จ !
( ์‹ค์Šต ์ฝ”๋“œ ๊ฐ™์ด ๋ณด๋ฉด์„œ ์ดํ•ดํ•˜๊ธฐ )


๐ŸŒˆ ๋Š๋‚€์ 

์˜ค๋Š˜ ๊ฐœ๋…๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๊ณ  ๋‹ค ๊ดœ์ฐฎ์•˜๋‹ค !
ํŽ˜์–ด์™€์˜ ์‹ค์Šต๋„ ๋น ๋ฅด๊ฒŒ ๋๋‚ฌ๊ณ  ์‹œ์ž‘์ด ์ข‹์•˜๋‹ค.
ํ•˜์ง€๋งŒ git์—์„œ push๋ฅผ ํ•  ๋•Œ ์ƒ๊ธด ์˜ค๋ฅ˜ ๋•Œ๋ฌธ์— ๋ช‡์‹œ๊ฐ„์„ ์žก์•„๋จน์€ ๊ฑด์ง€.. ใ… ใ… ใ… 
ํ•  ๊ฒŒ ๋งŽ๋‹ค ..

0๊ฐœ์˜ ๋Œ“๊ธ€