반응형

# 스프링 웹 개발 기초

## 정적 컨텐츠

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

  • 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>

## MVC와 템플릿 엔진

  • 서버에서 html 을 변형하여 동적으로 하는 것으로, 이를 위해 MVC (Model View Controller) 필요.
  • Controller
package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }

    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }
}
  • View
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
  • 확인 : http://localhost:8081/hello-mvc?name="고양이"

## API

  • JSON 데이터 구조 포멧으로 클라이언트에게 데이터 전달.
  • @Responsebody 문자 반환 : 뷰 리졸버(viewResolver)를 사용하지 않음, 대신 HTTP의 BODY에 문자 내용을 직접 반환.(HTML TAG를 말하는것 아님)
@Controller
public class HelloController {
     @GetMapping("hello-string")
     @ResponseBody	// 응답 바디에 직접 넣어주겠다는 것.
     public String helloString(@RequestParam("name") String name) {
         return "hello " + name;
     }
}
  • @Responsebody 객체 반환 : 객체를 반환하면 객체가 JSON 형식으로 변환 됨. 
package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }

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

    @GetMapping("hello-string")
    @ResponseBody   // 응답 바디에 직접 넣어주겠다는 것.
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name;
    }

    @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 this.name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
  • 출력결과 (JSON : key-value)
{"name":"\"호랑이\""}

  • @ResponseBody 사용 시
HTTP의 BODY에 문자 내용을 직접 반환

viewResolver 대신 HttpMessageConverter가 동작.

기본 문자처리 : StringHttpMessageConverter
기본 객체처리 : MappingJackson2HttpMessageConverter 
(Jackson : JSON 으로 바꿔는 라이브러리 중 하나, 스프링은 기본적으로 Jackson 탑재, 구글의 Gson도 존재함.)
byte 처리 등등 기타 여러 HttpMessageConverter가 기본적으로 등록되어 있다.
  • 클라이언트의 HTTP Accept 헤더와 서버의 컨트롤러 반환 타입 정보 둘을 조합해서 HttpMessageConverter가 선택됨. 
반응형

+ Recent posts