๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Spring

Spring Boot ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

by 1mj 2022. 3. 8.

Spring Boot ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

STS์—์„œ Spring Boot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

 

1. File > New > Spring Starter Project > Spring Starter Project ์„ ํƒ

  • Spring Starter Project: Spring Boot๋ฅผ ์ด์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ
  • Spring Legacy Project: ์ผ๋ฐ˜์ ์ธ Spring Framework ํ”„๋กœ์ ํŠธ

* Spring Starter Project๊ฐ€ ๋ณด์ด์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋งจ ์•„๋ž˜ Other... ์—์„œ ๊ฒ€์ƒ‰

 

2. New Spring Starter Project ์„ค์ •

ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •

 

3. New Spring Starter Project Dependencies ์„ค์ •

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ํŠน์ • ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜์กด์„ฑ์„ ์„ค์ •ํ•œ๋‹ค.

  • Spring Boot DevTools: Spring Boot์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ ํŽธ์˜๋ฅผ ์œ„ํ•œ ๋ชจ๋“ˆ
  • Spring Web: Spring์œผ๋กœ web์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ชจ๋“ˆ

Finish๋ฅผ ๋ˆŒ๋Ÿฌ ์„ค์ •์„ ์™„๋ฃŒํ•˜๋ฉด Progress ํƒญ์— Import Getting Started Content๊ฐ€ ๋œจ๋ฉด์„œ ์ž๋™์œผ๋กœ ์„ค์ • ๋ฐ ์„ธํŒ…์„ ํ•œ๋‹ค.

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด ํ”„๋กœ์ ํŠธ๋ช… ์šฐ์ธก์— [boot]๋ผ๊ณ  ๋œจ๊ฒŒ ๋˜๊ณ , ์˜์กด์„ฑ์—์„œ devTools๋ฅผ ์„ ํƒํ•œ ๊ฒฝ์šฐ [devtools] ๊นŒ์ง€ ์„ค์ •๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

4. ์ƒ์„ฑ๋œ Spring Boot ํ”„๋กœ์ ํŠธ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

์™ผ์ชฝ ํ•˜๋‹จ Boot Dashboard ํƒญ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒํ•œ ํ›„ ์‹คํ–‰ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

* ๋˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  Run As > Spring Boot App์„ ์„ ํƒํ•ด์„œ ์‹คํ–‰ํ•œ๋‹ค.

์‹คํ–‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Console ํƒญ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ , 8080 ํฌํŠธ(Default ํฌํŠธ)๋กœ ์‹คํ–‰๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

5. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ‘์†ํ•ด๋ณด๊ธฐ

์•ž์„œ 8080 ํฌํŠธ๋กœ ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Œ์„ ํ™•์ธํ–ˆ์œผ๋‹ˆ http://localhost:8080/ ๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ๊ธฐ๋ณธ ํ™”๋ฉด์ด ์ œ๊ณต๋˜์ง€ ์•Š์•„ ์—†๋Š” ํŽ˜์ด์ง€๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด ์ •์ƒ์ด๋‹ค.

 

6. ์ปจํŠธ๋กค๋Ÿฌ ๋งŒ๋“ค์–ด์„œ ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ

์ง๊ด€์ ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด controller ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— TestController.java ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜๊ณ  ์žฌ๊ธฐ๋™ํ•œ ํ›„ ๋ธŒ๋ผ์šฐ์ €์— ๋‹ค์‹œ ์ ‘์†ํ•ด๋ณด๋ฉด ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์ด ํ™”๋ฉด ์ƒ์— ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

* ์•„๋ž˜ ์ฝ”๋“œ ์ค‘ package~ ๋ถ€๋ถ„๊ณผ ํด๋ž˜์Šค๋ช…์€ ๋ณธ์ธ์ด ์ƒ์„ฑํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

package com.plus.tistory.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/")
    public String hello() {
        return "Hello Test Page!";
    }
}

7. ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ํฌํŠธ ๋ณ€๊ฒฝํ•˜๊ธฐ (๊ธฐ๋ณธ 8080 → 8081)

8080 ํฌํŠธ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉด ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ํฌํŠธ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค.

 

ํ”„๋กœ์ ํŠธ ์„ค์ • ํŒŒ์ผ์ธ application.properties์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์›ํ•˜๋Š” ํฌํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

server.port=8081

 

์ด์ œ http://localhost:8080/์ด ์•„๋‹Œ http://localhost:8081/๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ์•„๊นŒ์™€ ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋Œ“๊ธ€