01. SpringBoot - YAPP-16th/Team_Web_3_Backend GitHub Wiki

SpringBoot ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ค์ •


1. IntelliJ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

IntelliJ์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜๋Š” ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๋‹ค. IntelliJ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  IntelliJ IDEA > preference ํ•ญ๋ชฉ์œผ๋กœ ๋“ค์–ด๊ฐ€์„œ Spring ๋˜๋Š” Spring Boot๋กœ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋‚˜์˜จ ๊ฒฐ๊ณผ์—์„œ Spring Boot Plug-In ์„ ํƒํ•˜๊ณ  ์„ค์น˜ํ•œ๋‹ค.

2. SpringBoot ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

File -> New -> Project ๋ฅผ ํด๋ฆญํ•˜๋ฉด ํ”„๋กœ์ ํŠธ ํ…œํ”Œ๋ฆฟ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ SpringBoot Plug-in์„ ์„ค์น˜ํ–ˆ์œผ๋ฉด Spring Initializr ํ•ญ๋ชฉ์ด ๋ณด์ธ๋‹ค. ํ”„๋กœ์ ํŠธ ํƒ€์ž…์€ ํŽธํ•œ๊ฑธ๋กœ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค. ๋ณธ์ธ์€ Maven์— ์ต์ˆ™ํ•˜๋‹ˆ, Maven Project๋กœ ์„ ํƒํ–ˆ๋‹ค.

์ง€๊ธˆ์€ Embeded๋œ ํ†ฐ์บฃ์œผ๋กœ Stand-Aloneํ˜•ํƒœ๋กœ ๊ตฌ๋™์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ด๋ฏ€๋กœ Packagin Type์„ jar๋กœ ์„ ํƒํ•œ๋‹ค. war๋กœ ์„ ํƒํ•  ๊ฒฝ์šฐ, ๊ธฐ์กด๊ณผ ๊ฐ™์ด ์™ธ๋ถ€์˜ ํ†ฐ์บฃ์œผ๋กœ ๋””ํ”Œ๋กœ์ด ํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

๋ฌผ๋ก , ์†Œ์Šค์˜ ์ตœ์ข…๋ฐฐํฌ์˜ ํ˜•ํƒœ๊ฐ€ ์„œ๋ฒ„์˜ ํ†ฐ์บฃ์— ๋””ํ”Œ๋กœ์ดํ•ด์•ผ ํ•˜๋Š” ๊ตฌ์กฐ๋ผ๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ War๋กœ ๋งŒ๋“ค์–ด์„œ ์ž‘์—…ํ•ด๋„ ์ƒ๊ด€์—†๋‹ค. ์ง€๊ธˆ ์–ผ๋งˆ๋‚˜ ๋นจ๋ฆฌ, ๊ฐ„ํŽธํ•˜๊ฒŒ ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ˆ jar๋กœ ์„ ํƒํ•˜๊ณ , ๋กœ์ปฌ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋‹ค๊ฐ€ ๋‚˜์ค‘์— ๋ฐฐํฌํ•  ๊ฒฝ์šฐ war๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋ฐฐํฌ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

3. SpringBoot ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ ํƒ

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ํ›„ ๋‚˜์ค‘์— pom.xml๋กœ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ, ๊ผญ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋งŒ ์„ ํƒํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋ฉด ๋œ๋‹ค. ์„ ํƒํ•˜์ง€ ์•Š์•„๋„ ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค. Web์€ ์„ ํƒํ•ด์ฃผ๋Š”๊ฒƒ์ด ํŽธํ•˜๋‹ค. SpringBoot๋ฅผ ์ด์šฉํ•œ ํ†ฐ์บฃ๊ธฐ๋ฐ˜ ์›นํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

4. ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

  • ๋ฉ”์ธํด๋ž˜์Šค: SpringBootRestDemoApplication
  • ํ”„๋กœํผํ‹ฐํŒŒ์ผ: application.properties
  • pom.xml ํ†ฐ์บฃ๊ด€๋ จํ•œ ์„ค์ •๋ฐ web.xml๋“ฑ์˜ ํŒŒ์ผ์€ ๋ชจ๋‘ SpringBoot๊ฐ€ ๊ตฌ๋™๋ ๋•Œ ๋‚ด๋ถ€ ๋ชจ๋“ˆ์— ์˜ํ•ด์„œ AutoConfiquration ์ž๋™์„ค์ •๋œ๋‹ค. ์•ž์œผ๋กœ๋„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋งŒ์งˆ ์ผ์€ ์—†๋‹ค.

5. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰

๊ฐ„๋‹จํ•˜๋‹ค. ๋ฉ”์ธํด๋ž˜์Šค๋ฅผ Run์‹œํ‚ค๋ฉด SpringBoot ํŠน์œ ์˜ ๋กœ๊ณ ์™€ ํ•จ๊ป˜ ๋กœ๊ทธ๊ฐ€ ์˜ฌ๋ผ์˜ค๋ฉด์„œ ์‹คํ–‰๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฐ”๋กœ ์„ฑ๊ณต! ํ•˜๋ฉด ์ข‹์„ํ…๋ฐโ€ฆ. ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด์„œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘์ด ์‹คํŒจ๋œ๋‹ค.

Cannot determine embedded database driver class for database type NONE
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

ํ•ด์„ํ•ด๋ณด๋ฉด, ๋ฐ์ดํƒ€๋ฒ ์ด์Šค type์ด None์ด๋ผ ์ž„๋ฒ ๋””๋“œ๋œ ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์—†๋‹ค. ํ•˜๋‚˜์˜ ์ž„๋ฒ ๋””๋“œ๋œ ๋ฐ์ดํƒ€๋ฒ ์ด์Šค๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด ํด๋ž˜์ŠคํŒจ์Šค์— ์ง€์›๋˜๋Š” ๋“œ๋ผ์ด๋ฒ„ ํŒŒ์ผ์„ ๋„ฃ์–ด์•ผ ํ•˜๋ฉฐ profile๋กœ๋ถ€ํ„ฐ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ธํŒ…์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, profile์„ ํ™œ์„ฑํ™”์‹œ์ผœ์•ผ ํ•œ๋‹ค.

SpringBoot๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋ ๋•Œ ํ•„์š”ํ•œ ๊ธฐ๋ณธ ์„ค์ •๋“ค์„ ์ž๋™์œผ๋กœ ์„ค์ •ํ•˜๊ฒŒ ๋˜์–ด ์žˆ๋Š”๋ฐ (Auto Configuration) ๊ทธ ์ค‘ DataSource์„ค์ •์ด ์ž๋™๊ตฌ์„ฑ ๋  ๋•Œ ํ•„์š”ํ•œ Database Type ์ •๋ณด๋“ค์ด ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ application.propertiesํŒŒ์ผ์ด ์ž๋™ ์ƒ์„ฑ๋˜์—ˆ์œผ๋‚˜ ๋นˆ ํŒŒ์ผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” db๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ฑฐ๊ธฐ์— ๋งž๋Š” ๋“œ๋ผ์ด๋ฒ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜์™€ jdbc์„ค์ •์„ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐ์„ ํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜, ๋‹น์žฅ jdbc์„ค์ •์ด ํ•„์š”์—†๊ณ  ์–ด๋–ค db๋ฅผ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •๋„ ์•ˆ๋œ ์ƒํƒœ์ด๋‹ค.

์ด๋Ÿด ๊ฒฝ์šฐ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ?

DB๋ฅผ ๋‹น์žฅ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  SpringBoot๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ๊ตณ์ด DB์„ค์ •์„ ํ•˜์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด SpringBoot ๋ฉ”์ธํด๋ž˜์Šค์— Annotation์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ SpringBoot์‹œ์ž‘์‹œ ์‹คํ–‰๋˜๋Š” Auto Configuration์ž‘์—…์ค‘ DatatSource ์„ค์ •๋ถ€๋ถ„์„ ์ œ์™ธ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
์Šคํ”„๋ง๋ถ€ํŠธ ๋ฉ”์ธํด๋ž˜์Šค์— ์•„๋ž˜์™€ ๊ฐ™์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋‹ค์‹œ Run์‹œํ‚ค๋ฉด, ์ •์ƒ์ ์œผ๋กœ SpringBoot ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋œ๋‹ค. ํ•˜์ง€๋งŒ, ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด๊ณ  http://localhost:8080 ์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์˜ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๊ฐ€ ๋‚˜์˜จ๋‹ค.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Aug 13 14:34:00 KST 2017
There was an unexpected error (type=Not Found, status=404).
No message available

ํ•ด๋‹น ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ๋Š” ๋‹น์—ฐํžˆ ํ†ฐ์บฃ์ด ์ธ์‹ํ•ด์•ผ ํ•˜๋Š” index.html ํŒŒ์ผ์ด๋‚˜, index.jsp์ด ์•„์ง ์—†๊ธฐ ๋•Œ๋ฌธ๋‹ค.

5. SpringBoot ๋„์šฐ๊ธฐ

SpringBoot๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ Web๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ, ์ •์ ์ธ ์›น๋ฆฌ์†Œ์Šค๋Š” src > main > resources > static ์•„๋ž˜์—์„œ ๊ด€๋ฆฌ๋˜๋„๋ก ์„ค์ •๋œ๋‹ค. ์ฆ‰, html, css, js, image๋“ฑ์˜ ์ •์ ์ปจํ…์ธ ๋Š” static์•„๋ž˜์— ์œ„์น˜์‹œํ‚ค๋ฉด ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๊ณ , static์ด ๋ฃจํŠธํด๋”๊ฐ€ ๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋‹น์‹œ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ Web ๋ผ์ด๋ธŒ๋Ÿฌ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค๋ฉด static ํด๋”๊ฐ€ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ, ์„ ํƒํ•˜์ง€ ์•Š์•˜๋‹ค ํ•˜๋”๋ผ๋„ POM.xmlํŒŒ์ผ์— ์•„๋ž˜์˜ Dependency๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  staticํด๋”๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๋œ๋‹ค.

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifactid>
</dependency>

staticํด๋”์•„๋ž˜์— index.htmlํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  โ€˜Hello, SpringBoot!โ€™ ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์žฌ๊ตฌ๋™ํ•œ๋‹ค. http://localhost:8080 ๋กœ ์ ‘๊ทผํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ํŽ˜์ด์ง€์— ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


Reference

Markdown basic Syntax

โš ๏ธ **GitHub.com Fallback** โš ๏ธ