02B. ์ปจํธ๋กค๋ฌ ํ๋ก์ ํธ์ ๊ตฌ์กฐ
02B. ์ปจํธ๋กค๋ฌ ํ๋ก์ ํธ์ ๊ตฌ์กฐ ๊ด๋ จ
02. ์ปจํธ๋กค๋ฌ
์ด์ ์ฐจ๊ทผ์ฐจ๊ทผ ์คํ๋ง๋ถํธ ๊ฒ์ํ(SBB)์ ๋ง๋ค๋ฉด์ ์คํ๋ง๋ถํธ์ ๊ธฐ๋ฅ์ ํ๋์ฉ ์์๊ฐ ๋ณด์.
URL ๋งคํ
์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก SBB ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด ๋ณด์. ๋ธ๋ผ์ฐ์ ์์ http://localhost:8080/sbb
ํ์ด์ง๋ฅผ ์์ฒญํ์ ๋ "์๋
ํ์ธ์ sbb์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค."๋ผ๋ ๋ฌธ์์ด์ ์ถ๋ ฅํ๋๋ก ๋ง๋ค์ด ๋ณด์.
STS์ Boot Dashboard์ ์์ ๋ฒํผ์ ๋๋ฌ ๋ก์ปฌ์๋ฒ๋ฅผ ๋จผ์ ๊ตฌ๋ํ์.

http://localhost:8080/sbb
ํ์ด์ง๋ฅผ ์์ฒญํด ๋ณด์.
404 ์ค๋ฅ๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์์ฒญํ ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ ๊ฒฝ์ฐ์ ๋ฐ์ํ๋ค.
์ค๋ฅ๊ฐ ๋ฐ์ํ ์์ธ์ ์คํ๋ง๋ถํธ ์๋ฒ๊ฐ http://localhost:8080/sbb
๋ผ๋ ์์ฒญ์ ํด์ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ ๋ค๋ฉด ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ํด์ผ ํ ์ผ์ ๋ฌด์์ผ๊น? ์ปจํธ๋กค๋ฌ๋ฅผ ์์ฑํ๊ณ /sbb
URL์ ๋ํ ๋งคํ์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค. ํ์ด์ง ์์ฒญ์ด ๋ฐ์ํ๋ฉด ์คํ๋ง๋ถํธ๋ ๊ฐ์ฅ ๋จผ์ ์ปจํธ๋กค๋ฌ์ ์์ฒญ๋ ํ์ด์ง์ URL ๋งคํ์ด ์๋์ง๋ฅผ ์กฐ์ฌํ๋ค.
์ปจํธ๋กค๋ฌ
URL ๋งคํ์ ์ถ๊ฐํ๊ธฐ ์ํด MainController.java
ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์.
ํ์ผ๋ช :
/sbb/src/main/java/com/mysite/sbb/
MainController.java
package com.mysite.sbb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MainController {
@GetMapping("/sbb")
public void index() {
System.out.println("index");
}
}
MainController
ํด๋์ค์ @Controller
์ ๋ํ
์ด์
์ ์ ์ฉํ๋ฉด MainController
ํด๋์ค๋ ์คํ๋ง๋ถํธ์ ์ปจํธ๋กค๋ฌ๊ฐ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฉ์๋์ @GetMapping
์ ๋ํ
์ด์
์ ์์ฒญ๋ URL๊ณผ์ ๋งคํ์ ๋ด๋นํ๋ค. ์๋ฒ์ ์์ฒญ์ด ๋ฐ์ํ๋ฉด ์คํ๋ง๋ถํธ๋ ์์ฒญ ํ์ด์ง์ ๋งคํ๋๋ ๋ฉ์๋๋ฅผ ์ปจํธ๋กค๋ฌ๋ฅผ ๋์์ผ๋ก ์ฐพ๋๋ค. ์ฆ, ์คํ๋ง๋ถํธ๋ http://localhost:8080/sbb
์์ฒญ์ด ๋ฐ์ํ๋ฉด /sbb
URL๊ณผ ๋งคํ๋๋ index
๋ฉ์๋๋ฅผ MainController
ํด๋์ค์์ ์ฐพ์ ์คํํ๋ค.
@GetMapping
์http://localhost:8080
๊ณผ ๊ฐ์ ๋๋ฉ์ธ๋ช ๊ณผ ํฌํธ๋ ์ ์ง ์๋๋ค. ์๋ํ๋ฉด ๋๋ฉ์ธ๋ช ๊ณผ ํฌํธ๋ ์๋ฒ ์ค์ ์ ๋ฐ๋ผ ๋ณํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ด์ ๋ค์ http://localhost:8080/sbb
URL์ ํธ์ถํด ๋ณด์.

http://localhost:8080/sbb
ํธ์ถ์ MainController
์ index
ํจ์๋ ํธ์ถ๋์์ง๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. URL๊ณผ ๋งคํ๋ ํจ์๋ ๊ฒฐ๊ด๊ฐ์ ๋ฆฌํดํด์ผ ํ๋๋ฐ ์๋ฌด๋ฐ ๊ฐ๋ ๋ฆฌํดํ์ง ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด ํด๋ผ์ด์ธํธ(๋ธ๋ผ์ฐ์ )๋ก ์๋ต์ ๋ฆฌํดํด์ผ ํ๋ค.
์ฝ์ ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด index ๋ฉ์ค๋์์ ์คํํ
System.out.println("index");
๊ฐ ์คํ๋์ด "index"๋ผ๋ ๋ฌธ์์ด์ด ์ถ๋ ฅ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ค์๊ณผ ๊ฐ์ด MainController
๋ฅผ ์์ ํ์.
package com.mysite.sbb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@GetMapping("/sbb")
@ResponseBody
public String index() {
return "index";
}
}
์๋ต์ผ๋ก "index"
๋ผ๋ ๋ฌธ์์ด์ ๋ธ๋ผ์ฐ์ ์ ์ถ๋ ฅํ๊ธฐ ์ํด index ํจ์์ ๋ฆฌํด๊ฐ์ String
์ผ๋ก ๋ณ๊ฒฝํ๊ณ "index"
๋ผ๋ ๋ฌธ์์ด์ ๋ฆฌํดํ๋ค. @ResponseBody
์ ๋ํ
์ด์
์ URL ์์ฒญ์ ๋ํ ์๋ต์ผ๋ก ๋ฌธ์์ด์ ๋ฆฌํดํ๋ผ๋ ์๋ฏธ์ด๋ค.
๋ง์ฝ
@ResponseBody
์ ๋ํ ์ด์ ์ ์๋ตํ๋ค๋ฉด "index"๋ผ๋ ์ด๋ฆ์ ํ ํ๋ฆฟ ํ์ผ์ ์ฐพ๊ฒ ๋๋๋ฐ ์ด์ ๋ํ ๋ด์ฉ์ ๋์ค์ ์์ธํ ์์๋ณธ๋ค.

http://localhost:8080/sbb
URL์ ํธ์ถํด ํ์ธํด ๋ณด์.์ด๋ฒ์๋ MainController
์ ์ถ๋ ฅ ๋ฌธ์์ด "index"
๋ฅผ "์๋
ํ์ธ์ sbb์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค."๋ก ๋ฐ๊พธ์ด๋ณด์.
package com.mysite.sbb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@GetMapping("/sbb")
@ResponseBody
public String index() {
return "์๋
ํ์ธ์ sbb์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค.";
}
}

Spring Boot Devtools์ Live Reload ๊ธฐ๋ฅ์ ์ํด ์๋ฒ๋ ๋ฌผ๋ก ๋ธ๋ผ์ฐ์ ๋ ๋ฆฌ๋ก๋ฉ๋์ด ๋ณ๊ฒฝ๋ ์ฌํญ์ ์ถ๊ฐ ์์ ์์ด ์ฆ์ ํ์ธํ ์ ์์ ๊ฒ์ด๋ค.
์ฒซ๋ฒ์งธ ํ๋ก๊ทธ๋จ ์์ฑ
์ฌ๋ฌ๋ถ์ ์ฒซ๋ฒ์งธ SBB ํ๋ก๊ทธ๋จ์ ๋ง๋ค์๋ค. ์ถํํ๋ค!