정적 컨텐츠

    스프링 부트 정적 컨텐츠 기능을 자동으로 제공 - static

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

     

    • 실행 - http://localhost:8080/hello-static.html 

     

    정적 컨텐츠 이미지

     

    - 웹 브라우저에서 주소를 입력하면 내장 톰캣 서버에서 요청을 받고, hello-static.html의 요청이 왔다고 스프링 부트한테 넘김

    - 스프링 부트는 먼저 컨트롤러에서 hello-static 찾음

    - 없으면 resources/static 에서 hello-static.html 찾아서 웹 브라우저에 반환


    MVC와 템플릿 엔진

    • MVC: Model, View, Controller

     

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

     

    • View - resources/templates/hello-template.html 생성
    <html xmlns:th="http://www.thymeleaf.org">
    <body>
    <p th:text="'hello ' + ${name}">hello! empty</p>
    </body>
    </html>

     

    • 실행 - http://localhost:8080/hello-mvc?name=spring 

    MVC, 템플릿 엔진 이미지

    - 웹 브라우저에서 주소를 입력하면 내장 톰캣 서버에서 요청을 받고, hello-mvc의 요청이 왔다고 스프링 부트한테 넘김

    - 컨트롤러에 hello-mvc 가 매핑이 되어있으므로 hello-mvc 메서드 호출

    - hello-mvc?name=spring 이므로 값은 spring - model(name:spring)

    - retrun: hello-template이므로 viewResolver가 temlplates/hello-template.html을 템플릿 엔진에 넘김

    - 템플릿 엔진이 랜더링해서 변환한 HTML 반환


    API

    • @ResponseBody 문자 반환
    @Controller
     public class HelloController {
         @GetMapping("hello-string")
         @ResponseBody
         public String helloString(@RequestParam("name") String name) {
             return "hello " + name;
         }
     }

    - @ResponseBody를 사용하면 viewResolver를 사용하지 않음

     

    • 실행 - http://localhost:8080/hello-string?name=spring

    - HTTP의 BODY에 문자 내용을 직접 반환

     

    • @ResponseBody 객체 반환
    @Controller
    public class HelloController {
    	
        @GetMapping("hello-api")
        @ReponseBody
        public Hello helloApi(@RequestParam("name") String name) {
        	Hello hello = new Hello();
            hello.setName(name);
            return hello;
        }
        
        static class Hello {
        	private String name;
            
            pubilc String getName() {
            	return name;
            }
            
            public String setName(String name) {
            	this.name = name;
            }
        }
    }

    - Getter, Setter 필요

    - @ResponseBody를 사용

     

    • 실행 - http://localhost:8080/hello-api?name=spring

    - 객체를 반환하면 객체가 JSON으로 변환됨

     

     

    @ResponseBody 사용 원리

    - HTTP의 BODY에 문자 내용 직접 반환

    - viewResolver 대신에 HttpMessageConverter 가 동작

    - 기본 문자 처리(위의 문자 반환의 경우): StringHttpMessageConverter

    - 기본 객체 처리(위의 객체 반환의 경우): MappingJackson2HttpMessageConverter

    + Recent posts