๊น์ํ๋์ Spring ์์ ์ ๋ฃ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
๊ณต์ ์ฌ์ดํธ
๊ณต์๋ฌธ์์ Document, Getting Started ๋ฑ์ ์ฐธ์กฐํ ์ ์๋ค.
ํ๋ก์ ํธ ์์ฑ
Dependency - SpringWeb, ThymeLeaf
Gradle์ ๋ฒ์ ์ค์ , ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ธ์ค๊ณ , ๋ผ์ดํ ์ฌ์ดํด ๊ด๋ฆฌ๋ฅผ ์ํํ๋ค.
build.gradle
repository - mavenCentral ์ด๋ผ๋ ์ฌ์ดํธ์์ dependency๋ฅผ ๋ค์ด๋ก๋ํ๋ค.
dependencies - ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค ๊ฐ์ ธ์จ๋ค.
ํ๋ก์ ํธ๋ฅผ ์คํํ๋ฉด log: Tomcat Started on port(s) 8080 ๋ผ๋ ๋ก๊ทธ๊ฐ ๋จ๋๋ฐ, ์คํ๋ง์ด ๋ด์ฅํ๋ ์น ์๋ฒ๊ฐ TomCat์์ ์ ์ ์๋ค.
์คํ๋ง์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
External Libraries ๋ฅผ ๋ณด๋ฉด ์์ฒญ๋๊ฒ ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์จ ๊ฑธ ๋ณผ ์ ์๋ค.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
์์, starter-web์ ๊ทธ ์์ ํฐ์บฃ, ์น ๋ฑ ๋ ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์์กด์ ํ๊ณ ์๊ณ Gradle์ ์์กดํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค ๊ฐ์ ธ์ค๋ ์ญํ ์ ์ํํ๋ค.
์ฌ๊ธฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ์์กด๊ด๊ณ๋ฅผ ์ดํด๋ณผ ์ ์๋๋ฐ, ์คํ๋ง๋ถํธ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐ๋ฉด, ์คํ๋ง ์ฝ์ด๋ ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์๋ฒ์์ ๋ก๊น ์ญ์ ์ค์ํ๋ฐ, ์คํ๋ง ๊ฐ์ ๊ฒฝ์ฐ Gradle Dependencies๋ฅผ ๋ณด๋ฉด spring-boot-starter-loggin ์์ slf4j, logback ๋ฑ์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. (์์ฃผ ์ฌ์ฉ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์คํ๋ง ์ง์์์ ์ถ๊ฐํ ๊ฒ)
ํ ์คํธ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ง์ฐฌ๊ฐ์ง๋ก junit(ํ ์คํธ ํ๋ ์์ํฌ)๋ฑ์ด ์ถ๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. (๊ทธ ์ธ mockito(๋ชฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ), assertj(ํ ์คํธ ์ฝ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ), spring-test ๋ฑ์ด ์๋ค.)
View ํ๊ฒฝ ์ค์
- Welcome Page ๋ง๋ค๊ธฐ
- ์คํ๋ง์ src > resources > static์ index.html ํ์ผ์ ๋ง๋ค๋ฉด, ํด๋น ํ์ด์ง๊ฐ ๋ณด์ฌ์ง๋ค. (์ ์ ํ์ด์ง)
- thymeleaf ํ ํ๋ฆฟ ์์ง ์ ์ฉํ๊ธฐ (๊ทธ ์ธ FreeMarker, Groovy, Mustache ๋ฑ์ด ์๋ค)
- Web Application์์ ์ฒซ ๋ฒ์งธ ์ง์ ์ ์ Controller๋ผ ํ๋ค.
package com.kakaopay.springstudy.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello");
return "hello";
}
}
- hello.html
- ๊ฒฐ๊ณผ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<p th:text="'์๋
ํ์ธ์.' + ${data}"> ??? </p>
</body>
</html>
๋์ํ๊ฒฝ
- ์ปจํธ๋กค๋ฌ์์ ๋ฆฌํดํ String์ ViewResolver๊ฐ ํ๋ฉด์ ์ฐพ์ ์ฒ๋ฆฌํ๋ค.
- ์คํ๋ง ๋ถํธ ํ ํ๋ฆฟ์์ง ๊ธฐ๋ณธ viewName ๋งคํ
- resources: templates/{viewName}.html
- ์ฐธ๊ณ
- spring-boot-dedvtools ๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ฉด html ํ์ผ์ ์ปดํ์ผ๋ง ํด์ฃผ๋ฉด ์๋ฒ ์ฌ์์ ์์ด View ํ์ผ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
๋น๋์ ์คํ
$ ./gradlew build
$ cd build
$ java -jar hello-spring-0.0.1-SNAPSHOT.jar
๋น๋ ์ง์ฐ๊ธฐ
$ ./gradlew clean
์คํ๋ง ์น ๊ฐ๋ฐ ๊ธฐ์ด
- ์ ์ ์ปจํ
์ธ
- Spring์ Controller์ ๋งตํ๋ ๊ฒ ์๋ค๋ฉด static ํด๋์์ ์ ์ ํ์ผ์ ์ฐพ๋๋ค.
- MVC์ ํ
ํ๋ฆฟ ์์ง
- MVC๋ Model, View, Controller์ ์ค๋ง, ๊ด์ฌ์ฌ์ ๋ถ๋ฆฌ๋ฅผ ์ํจ (View๋ ํ๋ฉด์ ๊ทธ๋ฆฌ๋ ๊ฒ์๋ง, Model์ ๋ฐ์ดํฐ์ ํ์๋ง, Controller๋ ๋น์ฆ๋์ค ๋ก์ง๋ง ๊ด๋ฆฌ)
- Parameter ๋ฐ๊ธฐ
@GetMapping("hello-mvc")
public String helloMVC(@RequestParam(value = "name", required = false) String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
- API (๋ณดํต HTML์ด ์๋ JSON ํฌ๋ฉง์ผ๋ก ํด๋ผ์ด์ธํธ์๊ฒ ์ ๋ฌ, React, Android, Vue ํน์ ์๋ฒ์ ํต์ )
- @ResponseBody๋ Body์ ์ด ๋ฌธ์์ด์ ๋ฃ์ ๊ฒ์ ์๋ฏธํจ (๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ๋ณด๋ผ ๊ฒ์ด๋ผ๋ ๊ฑธ ์๋ฏธํจ, default๊ฐ true์ด๊ธฐ ๋๋ฌธ์ default=false๊ฐ ์๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ)
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
- JSON ํฌ๋ฉง์ผ๋ก ๋ฆฌํดํ๊ธฐ (default๊ฐ JSON ๋ฆฌํด)
- ResponseBody ์ด๋
ธํ
์ด์
์ด ์๋ค๋ฉด ViewResolver๊ฐ ์๋, HttpMessageConverter๊ฐ ์๋ํ๋ค.
- ๋จ์ ๋ฌธ์์ด์ด๋ฉด StringHttpMessageConverter ์๋๋ผ๋ฉด MappingJackson2HttpMessageConverter๊ฐ ์ฒ๋ฆฌ๋ฅผ ํ๋ค. (๋ฐ๋ผ์ ๊ฐ์ฒด๊ฐ JSON ํ์์ผ๋ก ๋ฐ๋์ด ๋ฆฌํด๋๋ ๊ฒ)
- ResponseBody ์ด๋
ธํ
์ด์
์ด ์๋ค๋ฉด ViewResolver๊ฐ ์๋, HttpMessageConverter๊ฐ ์๋ํ๋ค.
static class Hello {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
@GetMapping("hello-api")
@ResponseBody
public Hello helloJSON(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello;
}
- ๊ฒฐ๊ณผ