[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ

2021. 8. 5. 00:08ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์ฃผ๋‹ˆํ•˜๋ž‘์€ Spring Boot์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ธํ”„๋Ÿฐ์˜ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ํ”ผ๋“œ๋ฅผ ์ž‘์„ฑ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๋ฐ”๋กœ ์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 


 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 


 

 

๐Ÿ“Œ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—


์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 

 

๐Ÿ“Œ ์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ ๊ธฐ์ดˆ


  • ์ •์  ์ปจํ…์ธ  : ์„œ๋ฒ„๋ฅผ ํ†ตํ•˜์ง€ ์•Š๊ณ , html ๋ฌธ์„œ๋ฅผ ๊ทธ๋Œ€๋กœ Browser์— ์ถœ๋ ฅ.
  • MVC์™€ Templat Engin : ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹, ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ๋™์  ์ปจํ…์ธ ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹.
  • API : json์„ ํ†ตํ•ด Client - Server, Server - Server ๊ฐ„ Data๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹.

 

Spring Boot ์ •์  ์ปจํ…์ธ  ๊ธฐ๋Šฅ

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content

 

 

    ๐Ÿ“ ์ •์  ์ปจํ…์ธ  ๋ฐฉ์‹

<!DOCTYPE HTML>
<html>
<head>
	<title>static content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
This is Static Contents
</body>
</html>

์œ„์™€ ๊ฐ™์€ html File์„ /main/resources/static ๋ฐ‘์— ๋„ฃ๊ฒŒ ๋˜๋ฉด ๋œ๋‹ต๋‹ˆ๋‹ค!

 

 

 

์œ„์™€ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š” ์›๋ฆฌ๋Š” ๋จผ์ € Web Browser๊ฐ€ uri์— hello-static.html์„ ๋ณด๋‚ด๊ฒŒ ๋˜๋ฉด ํ†ฐ์ผ“ ์„œ๋ฒ„๊ฐ€ ๋จผ์ € ๊ทธ ๋‚ด์šฉ์„ ๋ฐ›์€ ๋’ค Spring Boot์—๊ฒŒ ์ „๋‹ฌ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.
Spring Boot๋Š” hello-static ๊ด€๋ จ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ์•„๋ณด๊ฒŒ ๋˜๋Š”๋ฐ,
์—†๋‹ค๋ฉด resouces/static์— ํ•ด๋‹น htmlํŒŒ์ผ์„ ์ฐพ์•„ ์‘๋‹ต ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

    ๐Ÿ“ MVC์™€ Templat Engin


  • MVC : Model, View, Controller

 

Controller

@Controller
public class HelloController {
	@GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
    model.addAttribute("name", name);
    return "hello-template";
    }
}

@RequestParam์€ uri์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ธ ๊ฒƒ์ด์—์š”.

"name"์ด๋ผ๊ณ  ์„ ์–ธ์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— uri ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…์€ name์ด ๋˜๊ณ , addAtribute()์—์„œ ๋’ค์— name์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ž…๋ ฅ ํ•ด ์ฃผ๋ฉด ์•„๋ž˜ html์—์„œ ${name}์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜์„œ ์‘๋‹ต์ด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

model.addAttribute๋Š” Key์™€ Value๊ฐ’์„ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐ›๋Š”๋ฐ, ์•ž์— "name" ๋ถ€๋ถ„์ด Key์ด๊ณ , ๋’ค์— name์ด value์ธ ๊ฒƒ์ด์—์š”.

์ด๊ฒƒ์„ ํ†ตํ•ด View์—๊ฒŒ ์ „๋‹ฌํ•  Data๋ฅผ Key์™€ Value๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

View

(resources/template/hello-template.html)

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text"'hello ' + ${name}"> hello empty</p>
</body>
</html>

 

 

<Browser ์†Œ์Šค ๋ณด๊ธฐ>

 

MVC, ํ…œํ”Œ๋ฆฟ ์—”์ง„์—์„œ๋Š” ์š”์ฒญ์— uri๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๊ทธ๊ฒƒ์„ ํ†ฐ์ผ“์ด Spring Boot์—๊ฒŒ ๋„˜๊ธฐ๊ณ , Boot๋Š” ํ•ด๋‹น Controller๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.
return๊ฐ’์ด 'hello-template"์œผ๋กœ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— templates ๋ฐ‘์— hello-template.html์ด ์žˆ๋Š”์ง€ ์ฐพ์•„ ๋ณด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๊ณ , model.addAttribute๋ฅผ ํ†ตํ•ด Key์™€ value๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š”๋ฐ, view์—์„œ๋Š” ์ด๊ฒƒ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 
uri์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฐ›์•„์„œ ์‘๋‹ต์œผ๋กœ ์ฒ˜๋ฆฌ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

@ResponseBody Entity ๋ฐ˜ํ™˜

@Controller
  public class HelloController {
      @GetMapping("hello-api")
      @ResponseBody
      public Hello helloApi(@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;
		  } 
	}
}

@ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Entity(๊ฐ์ฒด)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด Entity๊ฐ€ Jsonํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”. 

 

@ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด?

  • HTTP Body ๋ฌธ์ž ๋‚ด์šฉ์„ ์ง์ ‘ ๋ฐ˜ํ™˜
  • viewResolver ๋Œ€์‹  HttpMessageConverter๊ฐ€ ๋™์ž‘
  • ๊ธฐ๋ณธ ๋ฌธ์ž ์ฒ˜๋ฆฌ: StringHttpMessageConverter
  • ๊ธฐ๋ณธ ๊ฐ์ฒด ์ฒ˜๋ฆฌ: MappingJackson2HttpMessageConverter
    Jackson๋ง๊ณ , ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  Gson๋“ฑ ๋งŽ์€ ๊ฒƒ๋“ค์ด ์žˆ์œผ๋‚˜, Spring Boot๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Jackson์„ ์‚ฌ์šฉํ•˜๊ณ , ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
  • byte ์ฒ˜๋ฆฌ ๋“ฑ๋“ฑ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ HttpMessageConverter๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋“ฑ๋ก

 

๐Ÿ’ก ์ฐธ๊ณ 
Client์˜ HTTP Accpt Header์™€ Server์˜ Controller Return Type ์ •๋ณด๋ฅผ ์กฐํ•ฉํ•ด์„œ HttpMessageConvert๊ฐ€ ์„ ํƒ

 

 


์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•