Search

[springboot] ์Šคํ”„๋ง ์ž…๋ฌธ - 2. ์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ ๊ธฐ์ดˆ

ํƒ€์ž…
์Šคํ„ฐ๋””
ํƒœ๊ทธ
springboot
์ƒํƒœ
Published
์ƒ์„ฑ์ผ
2022/07/22 07:47
์ตœ์ข… ํŽธ์ง‘ ์ผ์‹œ
2023/02/27 03:14
์ƒ์œ„ ํ•ญ๋ชฉ
1 more property
์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜ ์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค

์›น ๊ฐœ๋ฐœ ๋ฐฉ์‹

1.
์ •์  ์ปจํ…์ธ 
2.
MVC ์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„
3.
API

์ •์  ์ปจํ…์ธ 

resources/static ํด๋”์— ์ •์  html ํŒŒ์ผ์„ ๋‘๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ ํ•ด๋‹น ํŒŒ์ผ๋ช… ์ฃผ์†Œ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๋‚ด๋ถ€ ๋™์ž‘ ์›๋ฆฌ
์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.
1.
ํด๋ผ์ด์–ธํŠธ ๋‹จ์—์„œ ์š”์ฒญ์ด ๋“ค์–ด์˜จ๋‹ค.
2.
์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํ•ด๋‹น ์š”์ฒญ๊ณผ ๋งคํ•‘๋ผ์žˆ๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค.
3.
๋งŒ์•ฝ ๋งคํ•‘๋œ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์—†์œผ๋ฉด static ํด๋”์˜ ์ •์ ํŒŒ์ผ์„ ์‘๋‹ตํ•œ๋‹ค.
resources/static/hello-static.html
<!DOCTYPE HTML> <html> <head> <title>static content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> ์ •์  ์ปจํ…์ธ  ์ž…๋‹ˆ๋‹ค. </body> </html>
HTML
๋ณต์‚ฌ

MVC์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„

Controller

HelloController.java
@Controller public class HelloController { @GetMapping("hello-mvc") public String helloMvc(@RequestParam(value = "name") String nameValue, Model model){ model.addAttribute("name", nameValue); return "hello-template"; } }
Java
๋ณต์‚ฌ

View

templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org"> <body> <p th:text="'hello ' + ${name}"></p> </body> </html>
HTML
๋ณต์‚ฌ

๊ฒฐ๊ณผ

์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ์—†์œผ๋ฉด error page ๊ฐ€ ๋œฌ๋‹ค.
@RequestParam ์—์„œ required=false ์˜ต์…˜์„ ์ฃผ๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์„ ์‹œ null ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
@Controller public class HelloController { @GetMapping("hello-mvc") public String helloMvc(@RequestParam(value = "name", required = false) String nameValue, Model model){ model.addAttribute("name", nameValue); return "hello-template"; } }
Java
๋ณต์‚ฌ
์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ name=spring! ์„ ๋„˜๊ฒผ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ
thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„์—์„œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›์•„์„œ spring! ์„ ์ถœ๋ ฅํ•ด์ฃผ๊ณ  ์žˆ๋‹ค.

๋‚ด๋ถ€๋™์ž‘

API ํ†ต์‹  ๋ฐฉ์‹

response๋กœ ๋ฌธ์ž์—ด ๋ฆฌํ„ด

@Controller public class HelloController { @GetMapping("hello-string") @ResponseBody public String helloString(@RequestParam("name") String nameValue) { return "hello " + nameValue; // ๋ฌธ์ž์—ด ๋ฆฌํ„ด } }
Java
๋ณต์‚ฌ
@ResponseBody ๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‹ฌ์•„์ฃผ๋ฉด return ๊ฐ’์ด http response ์˜ body ๊ฐ’์ด ๋œ๋‹ค.
์š”์ฒญ๊ฒฐ๊ณผ

response ๋กœ json ๋ฆฌํ„ด

๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด ์‹œ, Json ์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ response body ๋กœ ๋„˜๊ฒจ์ค€๋‹ค. (์Šคํ”„๋ง๋ถ€ํŠธ ๋””ํดํŠธ์„ค์ •)
@Controller public class HelloController { @GetMapping("hello-api") @ResponseBody public Hello helloApi(@RequestParam("name") String nameValue) { Hello hello = new Hello(); hello.setName(nameValue); return hello; // hello ๊ฐ์ฒด๋ฅผ json ์œผ๋กœ ๋ฆฌํ„ด } static class Hello { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } }
Java
๋ณต์‚ฌ
ย Hello ํด๋ž˜์Šค์˜ name ๋ฉค๋ฒ„๋Š” private ์ด๋ฏ€๋กœ public ํ•œ ๋ฉ”์„œ๋“œ์ธ getter ์™€ setter ๋กœ name ๋ฉค๋ฒ„์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
ย getter, setter ์ž๋™์™„์„ฑ์€ cmd + n ์„ ํ†ตํ•ด์„œ ๊ฐ€๋Šฅํ•˜๋‹ค.
์š”์ฒญ๊ฒฐ๊ณผ

๋‚ด๋ถ€ ๋™์ž‘ ์›๋ฆฌ