[Spring] 2.์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ ๊ธฐ์ดˆ

 

0. ์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ ๊ธฐ์ดˆ

- ์ •์  ์ปจํ…์ธ  : ์„œ๋ฒ„์—์„œ ํ•˜๋Š” ๊ฒƒ ์—†์ด ํŒŒ์ผ์„ ์›น๋ธŒ๋ผ์šฐ์ €์— ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค์ฃผ๋Š” ๊ฒƒ

- MVC์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„ : HTML์„ ๊ทธ๋ƒฅ ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์„œ๋ฒ„์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ด์„œ ์ฃผ๋Š” ๊ฒƒ 

   = Model View Controller

- API : JSON(๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํฌ๋งท)์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ = React, ์„œ๋ฒ„ํ†ต์‹ 


1. ์ •์  ์ฝ˜ํ…์ธ 

- ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต

๋งํฌ : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web 

- resources - static

hello-static.html

์ž‘์„ฑ

๋™์ž‘์›๋ฆฌ : ์›น๋ธŒ๋ผ์šฐ์ € -> localhost8080/hello-static -> ๋‚ด์žฅ ํ†ฐ์บฃ์„œ๋ฒ„ ์š”์ฒญ ๋ฐ›์Œ -> ์Šคํ”„๋ง ๋„˜๊น€ ->

1) ์Šคํ”„๋ง์€ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํ™•์ธ (์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์šฐ์„ ์ˆœ์œ„) -> ์—†์Œ ->

2) ๋‚ด๋ถ€ resources์•ˆ์—์„œ ์ฐพ์Œ -> ๋ธŒ๋ผ์šฐ์ € ๋กœ ๋ฐ˜ํ™˜


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

- Model Veiw Controller

 

[Controller]

- ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ง‘์ค‘

- ๋‚ด๋ถ€ ์ฒ˜๋ฆฌ ์ง‘์ค‘

Hello Controller

    // ์œ„์—์„œ๋Š” string์„ ์ง์ ‘ ๋ฐ›์Œ
    // ์—ฌ๊ธฐ์„œ๋Š” @RequestParam์„ ์ด์šฉ,
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";

@RequestParam("name")

 

[View]

- ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๋Š”๋ฐ ์—ญ๋Ÿ‰ ์ง‘์ค‘

hello-template.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!--์•ˆ๋…•ํ•˜์„ธ์š” ์†๋‹˜ = ์„œ๋ฒ„์—†์ด html ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด-->
<p th:text="'Hello. ' + ${name}">์•ˆ๋…•ํ•˜์„ธ์š” ์†๋‹˜</p>
</body>
</html>

 

-> ์ฒ˜์Œ์— ์˜ค๋ฅ˜ ๋ฐœ์ƒ (ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์„ค์ • ์•ˆ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ)

-> ์ฃผ์†Œ์ฐฝ์— url ๋’ค์— ?name=spring

ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด addAtrribute์˜ value๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ  ๊ทธ๊ฒŒ attributeName์— ์ €์žฅ๋ผ์„œ ํ™”๋ฉด์— ๋‚˜์˜ค๋Š” ๊ฒƒ

= ${} ๋ชจ๋ธ์—์„œ ๊ฐ’์„ ๊บผ๋‚ด๋Š” ๊ฒƒ์ž„

 

๋™์ž‘์›๋ฆฌ

์›น๋ธŒ๋ผ์šฐ์ € -> localhost8080/mvc -> ๋‚ด์žฅ ํ†ฐ์บฃ์„œ๋ฒ„ -> ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋งตํ•‘๋จ์„ ํ™•์ธ -> returnํ• ๋•Œ hello-template์™€ model(name : spring) -> viewResolver (ํ™”๋ฉด ํ•ด๊ฒฐ์ž, ๋ทฐ ์ฐพ์•„์ฃผ๊ณ  ํ…œํ”Œ๋ฆฟ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ) -> ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด ์ฒ˜๋ฆฌ=๋ Œ๋”๋ง -> ๋ณ€ํ™˜ ํ›„ HTML ์„ ์›น๋ธŒ๋ผ์šฐ์ €์— ๋ฐ˜ํ™˜ 

(์ •์ ์ฝ˜ํ…์ธ ์™€์˜ ์ฐจ์ด์ ์ž„)


3. API

@ResponseBody

@GetMapping("hello-string")
    @ResponseBody
    public String helloString(@RequestParam("name") String name, Model model) {
        return "hello" + name;
    }

-> @ResponseBody : http์˜ body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋„ฃ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ

-> ๋ทฐ๊ฐ€ ์—†๊ณ  ๊ทธ๋ƒฅ ๋ฌธ์ž๊ฐ€ ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค๊ฐ

ex) ์ฃผ์†Œ์— ?name=spring (HTML ํƒœ๊ทธ ๊ฐ™์€๊ฑฐ ์—†์Œ)

 

ํด๋ž˜์Šค

   @GetMapping("hello-class")
    @ResponseBody
    public Hello helloAip(@RequestParam("name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }

    static class Hello {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

{ "name" : "spring"}

-> JSON ๋ฐฉ์‹์œผ๋กœ ๋‚˜์˜ด

(๊ณผ๊ฑฐ xml = html ํƒœ๊ทธ ์‚ฌ์šฉ)

 

-> ์Šคํ”„๋ง์€ ๊ฐ์ฒด๋ฅผ JSON ์œผ๋กœ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ์ž„

 

Getter Setter

- java bean ๊ทœ์•ฝ, java bean ํ‘œ์ค€๋ฐฉ์‹ = property ์ ‘๊ทผ ๋ฐฉ์‹

   = private ์™ธ๋ถ€์—์„œ ๋ชป๊บผ๋‚ด๊ฒŒ ํ•ด์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋„๋ก

 

๋™์ž‘์›๋ฆฌ

localhost8080/hello-api -> ํ†ฐ์บฃ ๋‚ด์žฅ์„œ๋ฒ„ -> ์ปจํŠธ๋กค๋Ÿฌ O -> @ResponseBody -> ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด -> ๋””ํดํŠธ : JSON ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“ค๊ธฐ (httpmessageconverter / ๋‹จ์ˆœ ๋ฌธ์ž๋ฉด string converter,  ๊ฐ์ฒด๋ฉด json converter) -> ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ต

 

[์ •๋ฆฌ]

@ResponseBody ์‚ฌ์šฉ

- HTTP์˜ BODY์— ๋ฌธ์ž ๋‚ด์šฉ์„ ์ง์ ‘ ๋ฐ˜ํ™˜

- 'viewResolver'๋Œ€์‹ ์— 'HttpMesseageConverter' ๊ฐ€ ๋™์ž‘

- ๊ธฐ๋ณธ๋ฌธ์ž์ฒ˜๋ฆฌ : StringHttpMessageConverter

- ๊ธฐ๋ณธ๊ฐ์ฒด์ฒ˜๋ฆฌ : MappingJackson2HttpMessageConverter (Jackson = ๊ฐ์ฒด๋ฅผ JSON์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” library)

  * jackson ๋ง๊ณ ๋„ ๊ตฌ๊ธ€์˜ GSON

- byte ์ฒ˜๋ฆฌ ๋“ฑ๋“ฑ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ HttpMessageConverter๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋“ฑ๋ก๋˜์–ด์žˆ์Œ

 

* ์ฐธ๊ณ : ํด๋ผ์ด์–ธํŠธ์˜ HTTP Accept ํ—ค๋”์™€ ์„œ๋ฒ„์˜ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ˜ํ™˜ ํƒ€์ž… ์ •๋ณด ๋‘˜์„ ์กฐํ•ฉํ•ด์„œ

'HttpMessageConverter' ๊ฐ€ ์„ ํƒ๋จ

  = Accept ํ—ค๋”์— xml, json ์ •๋ณด ์žˆ์œผ๋ฉด ํ•ด๋‹น ์ •๋ณด๋กœ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด ์„œ๋ฒ„ ์ปจํŠธ๋กค๋Ÿฌ

 

* cmd + shift + enter = ์™„์„ฑ์‹œ์ผœ์คŒ
* cmd + N = ์ฝ”๋“œ์ƒ์„ฑ (getter setter)
* cmd + P = ํŒŒ๋ผ๋ฏธํ„ฐ ์ •๋ณด ์˜ต์…˜

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ