2022-05-28
1. Spring web ๊ฐ๋ฐ ๋ฐฉ๋ฒ ์ข ๋ฅ
- (1) ์ ์ ์ปจํ ์ธ
- (2) MVC์ ํ ํ๋ฆฟ ์์ง
- (3) API
2. ์ ์ ํ์ด์ง์ ๋์ ํ์ด์ง์ ์ฐจ์ด
- ์ ์ ํ์ด์ง: ์น ์๋ฒ์ ์ด๋ฏธ ์ ์ฅ๋ html๋ฌธ์๋ฅผ client์๊ฒ ์ ์กํ๋ ์น ํ์ด์ง
- ์ฅ์ : (1)์์ฒญ์ ๋ํ ํ์ผ๋ง ์ ์กํ๋ฉด ๋๋ฏ๋ก ์๋๊ฐ ๋น ๋ฅด๋ค., (2)๋ค๋ฅธ๊ฑฐ ์์ด ์น์๋ฒ๋ง ๊ตฌ์ถํ๋ฉด ๋๋ฏ๋ก ๋น์ฉ์ด ์ ๊ฒ ๋ ๋ค.
- ๋จ์ : (1)์ ์ฅ๋ ์ ๋ณด๋ง ๋ณด์ฌ์ฃผ๋ฏ๋ก ์๋น์ค๊ฐ ํ์ ์ ์ด๋ค., (2)์์ฑ, ์์ , ์ญ์ ์ ์์ ์ด ๋ชจ๋ ์๋์ด๋ฏ๋ก ๊ด๋ฆฌ๊ฐ ํ๋ค๋ค.
- ๋์ ํ์ด์ง: ์์ฒญ ์ ๋ณด๋ฅผ ๊ฐ๊ณต์ฒ๋ฆฌํ ํ์ html ๋ฌธ์๋ฅผ ์์ฑํ์ฌ client์๊ฒ ์ ์กํ๋ ์น ํ์ด์ง
- ์ฅ์ : (1)๋ค์ํ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๋์ ์์ฑํ์ฌ ์ ๊ณตํ๋ฏ๋ก ์๋น์ค๊ฐ ๋ค์ํด์ง๋ค., (2)์น ์ฌ์ดํธ ๊ตฌ์กฐ์ ๋ฐ๋ผ ์์ฑ, ์์ , ์ญ์ ๋ฑ์ ์์ ์ด ์ฉ์ดํ๋ค.
- ๋จ์ : ๊ฐ๊ณต์ฒ๋ฆฌ ๊ณผ์ ๋๋ฌธ์ ์๋์ ์ผ๋ก ๋๋ฆฌ๋ค., ์น ์๋ฒ ์ด์ธ์ ์ถ๊ฐ์ ์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ์ํ WAS(Web Application Server)๊ฐ ํ์ํ๋ค.
3. ์ ์ ํ์ด์ง
- https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content
- ์ ๊ฒ์๋ฌผ์์ ํ๋ ๊ฒ๊ณผ ๊ฐ์ด resources/static ํด๋์ hello.html ์ ์ ํ์ด์ง๋ฅผ ์์ฑํ๋ฉด ์๋์ผ๋ก ๋งคํ๋์ด http://localhost:8080/hello.html๋ก ์ด๋ํ๋ฉด ํ์ธ ํ ์ ์์.
- ๊ฐ๋ตํ ๋์๊ณผ์ : http://localhost:8080/hello.html ์์ฒญ์ด ์ค๋ฉด ๋ด์ฅ ํฐ์บฃ ์๋ฒ๊ฐ spring์๊ฒ ๋๊ธฐ๋๋ฐ spring์ ์ฐ์ ์ปจํธ๋กค๋ฌ์์ hello๊ด๋ จ ์ปจํธ๋กค๋ฌ๋ฅผ ํ์ํ๊ณ ์กด์ฌํ์ง ์์ผ๋ฉด resources: static/hello.html ์ ํ์ํ์ฌ ๋ฐํํด์ค๋ค.
(์ค๊ฐ์ ๋ ๋ง์ ๊ณผ์ ์ด ์์ง๋ง ๊ฐ๋ตํ๊ฒ ์ค๋ช ํ ๊ฒ์.)
4. MVC์ ํ ํ๋ฆฟ ์์ง
- ๊ณผ๊ฑฐ์ ๋ง์ด ์ฐ๋ JSP์ PHP ๊ฐ์๊ฒ ๋์ ํ์ด์ง๋ฅผ ์์ฑํ๊ธฐ ์ํ ํ ํ๋ฆฟ ์์ง์ด๋ค.
- ๋์ ํ์ด์ง ๊ฐ๋ฐ์ ์ํด์ Model, View, Controller๊ฐ ํ์
- ํ ํ๋ฆฟ ์์ง์ ์ด์ฉํ์ฌ View๋ฅผ Controller๋ก ์กฐ์ํ์ฌ ๋์ ํ์ด์ง๋ก ๋์ํ๋๋ก ํด์ค.
๊ณผ๊ฑฐ jsp์์๋ view์ controller๊ฐ ๋ถ๋ฆฌ๋์ง ์๊ณ view์์ ๋ชจ๋ ๊ฒ์ ํ ๊ณณ์์ ํ๋ก๊ทธ๋๋ฐํ๋ Model1 ๋ฐฉ์์ ๋ง์ด ์ฌ์ฉํ์๋๋ฐ ํ์ฌ๋ MVCํจํด์ ๋ง์ด ์ฌ์ฉํ๋ค!
- View๋ ๊ทธ์ ํ๋ฉด์ ๊ทธ๋ฆฌ๋๋ฐ ๋ชจ๋ ์ํฅ์ ์ง์คํด์ผ ํจ.
- Controller Business Logic๊ณผ ๊ด๋ จ๋๊ฑฐ๋ ์ด๋ค ๋ด๋ถ์ ์ธ ๋ก์ง์ ์ฒ๋ฆฌํ๋๋ฐ ์ง์คํด์ผ ํจ
- Model์ ํ๋ฉด์์ ํ์ํ ๊ด๋ จ๋ ๊ฒ๋ค์ ๋ด์์ ์ ๋ฌํ๋๋ฐ ์ฌ์ฉ
๋ฐ๋ผ์ ๋ถ๋ฆฌํด์ผ ํจ!
HelloController.java
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name", name);
return "hello-template";
}
resources/templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
pํ๊ทธ ์์ <th:text="'hello ' + ${name}">hello! empty ๋ {name}๊ฐ์ด ์๋ฒ์์ ๋์ด์ค๋ฉด hello! empty๋ผ๋ default๊ฐ์ด ์๋ก์ด ๊ฐ์ผ๋ก ๋์ฒด๋จ
http://localhost:8080/hello-mvc?name=spring์ ๊ฐ์ด ๋ค์ param์ ๊ฐ์ด ๋๊ฒจ์ฃผ๋ฉด controller์์ Model์ ์์ฑ์ ์ถ๊ฐํ์ฌ ํ๋ฉด์์ View์์ ์ฌ์ฉํ ์ ์์.
๊ฐ์ ์ค ๊ฟํ: intellij์์ ctrl + p ๋๋ฅด๋ฉด parameter ์ ๋ณด ์ป์ ์ ์์.
5. API(Application Programming Interface)
- React, Vue ๊ฐ์ frontend framework ์ธ ๋๋ api๋ก ๋ฐ์ดํฐ๋ง ๋ด๋ ค์ฃผ๋ฉด ํ๋ฉด์ client๊ฐ ์์์ ๊ทธ๋ฆด ๋ ์ฌ์ฉ
- ์๋ฒ๋ผ๋ฆฌ ๋ฐ์ดํฐ ํต์ ํ ๋ ์ฌ์ฉ
//Controllerํด๋์ค ๋ด๋ถ
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello;
}
- @ResponseBody
- HTTP ํต์ ๋ด์์ Header์ Body์ค์์ Body์ return ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ฃ์ด์ฃผ๊ฒ ๋ค!
- viewResolver ๋์ ์ HttpMessageConverter๊ฐ ๋์
- ๊ธฐ๋ณธ ๋ฌธ์์ฒ๋ฆฌ๋ StringHttpMessageConverter
- ๊ธฐ๋ณธ ๊ฐ์ฒด์ฒ๋ฆฌ๋ MappingJackson2HttpMessageConverter
- HTTP ํต์ ๋ด์์ Header์ Body์ค์์ Body์ return ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ฃ์ด์ฃผ๊ฒ ๋ค!
๊ฐ์ฒด๋ฅผ json์ผ๋ก ๋ณํํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋๊ฐ Jackson!(Spring์ Default)
์๋ฒ๋ฅผ ์คํํ๊ณ ํ์ธํด๋ณด๋ฉด
์ ๊ฐ์ด Spring์ default ํ์์ผ๋ก json ํ์์ผ๋ก ๋์ค๊ฒ ๋จ.
XML์ ์ด๊ณ ๋ซ๋ ํ๊ทธ๋ฅผ ์จ์ผํด์ ๋ถํธํ๋ฐ JSON์ key valueํ์์ผ๋ก ๊ฐ๋จํด์ ํธํ ๊ฒ ๊ฐ๋ค!
๋์์๋ฆฌ
http api์์ฒญ์ด ๋ค์ด์ค๋ฉด
๋ด์ฅ ํฐ์บฃ ์๋ฒ๊ฐ ์คํ๋ง์๊ฒ ์๋ ค์ฃผ๊ณ ํด๋น api ๊ด๋ จ ๋ฉ์๋๊ฐ ์๋์ง ํ์ธํ๋๋ฐ ๊ทธ ๋ฉ์๋์ @ResponseBody๊ฐ ๋ถ์ด์๋ค๋ฉด HTTP ์๋ต์ผ๋ก ์ ํด์ฃผ๋๋ฐ ์ด ๊ณผ์ ์์ HttpMessageConverter๊ฐ ๋์ํ์ฌ ๋ฉ์๋๊ฐ ๋จ์ํ ๋ฌธ์์ด์ ๋ฐํํ๋ค๋ฉด StringConverter๊ฐ ๋์ํ์ฌ ๋ฌธ์์ด๋ก, ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ค๋ฉด JsonConverter๊ฐ ๋์ํ์ฌ json ํํ๋ก ๊ฐ์ฒด๋ฅผ ์์ฒญํ ์๋ฒ๋ ํด๋ผ์ด์ธํธ๋ก ์๋ตํ๋ค.
'๐ Backend > ๐ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
6. Spring Bean ๋ฑ๋ก (0) | 2022.07.02 |
---|---|
5. Spring ๊ฐ๋จํ ํ์๐โโ๏ธ ๊ด๋ฆฌ ์์ (0) | 2022.07.02 |
3. Spring Library๐์ ํ๋ก์ ํธ ์ค์ (0) | 2022.07.01 |
2. Spring ํ๋ก์ ํธ ์์ฑํ๋๋ฒ (0) | 2022.07.01 |
1. Spring ๊ณต๋ถ ์์! (1) | 2022.07.01 |