์ค๋ ๋ฐฐ์ด ํต์ฌ 3์ค ์์ฝ
@Controller + @GetMapping์ผ๋ก ์์ฒญ URL๋ง๋ค ๋ค๋ฅธ ๋ฐ์ดํฐ/ํ์ด์ง ์ ์ก ๊ฐ๋ฅ
@ResponseBody๊ฐ ์์ผ๋ฉด โ ๋ฌธ์์ด/๋ฐ์ดํฐ ๊ทธ๋๋ก ์ ์ก, ์์ผ๋ฉด โ html ํ์ผ ๋ฐํ
- ์ ์ ํ์ผ(html/css/js)์
resources/static/์ ๋๋ฉด ์๋ ์ ๊ณต๋จ
โ
์ด๋ค ๊ธฐ๋ฅ์ ๋ง๋๋ ๊ฒ์ธ๊ฐ?
- ์ ์ ๊ฐ
/, /about, /date ๊ฐ์ ๊ฒฝ๋ก๋ก ์์ฒญํ๋ฉด ์๋ฒ๊ฐ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ฃผ๋ ๊ธฐ๋ฅ
- ๊ฐ๋จํ ๋ฌธ์์ด, HTML ์ฝ๋, ํน์ ์ ์ HTML ํ์ผ์ ์ ์ก
๐ ์ ์ด๊ฑธ ๋ฐฐ์์ผ ํ์ง?
- ์น์๋ฒ์ ํต์ฌ์ โ์์ฒญ โ ์๋ตโ ๊ตฌ์กฐ
- Spring Boot๋
@Controller์ @GetMapping๋ง ์จ๋ URL ๋ผ์ฐํ
์ด ๊ฐ๋ฅ
- ์ด ํจํด์ด CRUD, ๋ก๊ทธ์ธ, ๊ฒ์ํ ๊ฐ์ ๊ธฐ๋ฅ์ ๊ธฐ๋ณธ ๋ผ๋๊ฐ ๋จ
๐ ๊ฐ๋
์ ๋ฆฌ
| ๊ฐ๋
| ์ค๋ช
|
|---|
@Controller | ์ด ํด๋์ค๊ฐ ์น ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ญํ ์ ํ๋ค๋ ์ ์ธ |
@GetMapping("/๊ฒฝ๋ก") | ํด๋น URL ๊ฒฝ๋ก๋ก ์์ฒญ์ด ๋ค์ด์ค๋ฉด ํจ์๋ฅผ ์คํ |
@ResponseBody | ํจ์์ return ๊ฐ์ ๋ฐ์ดํฐ๋ก ์ง์ ์๋ต |
| static ํด๋ | resources/static์ ์๋ html/css/js๋ ์๋ ์๋น |
| View ๋ฐํ | @ResponseBody ์ ๊ฑฐ + return์ html ํ์ผ๋ช
์ฐ๋ฉด โ ํด๋น ํ์ผ ๋ฐํ |
โ๏ธ ๊ตฌํ ํ๋ฆ ๋ฐ ์ฝ๋
๐ BasicController.java
package com.apple.shop;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BasicController {
@GetMapping("/")
@ResponseBody
String hello() {
return "์๋
ํ์ผ";
}
@GetMapping("/about")
@ResponseBody
String about() {
return "ํผ์ฑ์ฌ์ดํธ์์";
}
@GetMapping("/date")
@ResponseBody
String date() {
return java.time.ZonedDateTime.now().toString();
}
}
๐ resources/static/index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>๋ฉ์ธํ์ด์ง</title>
</head>
<body>
<h4>๋ฉ์ธํ์ด์ง์ธ๋ฐ์</h4>
</body>
</html>
๐ BasicController.java (html ๋ฐํ)
@Controller
public class BasicController {
@GetMapping("/")
String hello() {
return "index.html";
}
}
๐ ์ฌ์ฉ๋ ๊ฐ๋
์์ฝ
| ํค์๋ | ํ ์ค ์์ฝ |
|---|
@Controller | ์น ์์ฒญ ๋ด๋น ํด๋์ค ์ ์ธ |
@GetMapping | URL ๋ผ์ฐํ
(GET ์์ฒญ ์ฒ๋ฆฌ) |
@ResponseBody | return์ ๋ฐ์ดํฐ ๊ทธ๋๋ก ์ ์ก |
| static | ์ ์ ๋ฆฌ์์ค(html/css/js) ๋ณด๊ด ๊ฒฝ๋ก |
| ZonedDateTime.now() | ํ์ฌ ๋ ์ง/์๊ฐ ๊ฐ์ ธ์ค๊ธฐ |
๐ก ์ด๋ฐ ๊ณณ์ ํ์ฉํ ์ ์์ด์
- ํํ์ด์ง ๋ฉ์ธํ๋ฉด, ์๊ฐํ์ด์ง, ๋ง์ดํ์ด์ง ๋ฑ ๊ฒฝ๋ก๋ณ ์๋ต ๋ง๋ค๊ธฐ
- JSON ๋ฐ์ดํฐ(API) ์๋ตํ๊ธฐ
- ์ ์ ํ์ด์ง(๋ก๊ทธ์ธ, ํ์๊ฐ์
ํผ ๋ฑ) ๋ฐฐ์น
โ๏ธ ๊ฐ์ธ ์ ๋ฆฌ ๋ฐ ํ๊ณ
- ์น์๋ฒ๋ ๊ฒฐ๊ตญ โ์์ฒญ โ ์๋ตโ์ ์ฝ๋๋ก ํํํ ๊ฒ๋ฟ์ด๋ผ ๋จ์ํ๋ค.
@ResponseBody ์ ๋ฌด๋ก โ๋ฐ์ดํฐ ์ ์ก vs ํ์ผ ์ ์กโ์ด ๊ฐ๋ฆฐ๋ค.
/date ๊ฐ์ ๊ฐ๋จํ ๊ธฐ๋ฅ๋ ๋ฐ๋ก ์ถ๊ฐ ๊ฐ๋ฅํด์, ์์ ๊ธฐ๋ฅ ๋จ์๋ก ํ์ฅํ๋ ํ๋ฆ์ด ๋ณด์๋ค.
- ์์ผ๋ก CRUD๋ ์ด๋ฐ ๋ผ์ฐํ
ํจํด์ ๋ฐ๋ณตํด์ ๋ง๋ค๋ฉด ๋๋ค๊ณ ๋๊ผ๋ค.