본문 바로가기
Framekwork/SPRING

[SPRING] 스프링 부트 - 스프링 웹 개발 기초

by 아이엠제니 2023. 8. 29.

 

 


 

정적 컨텐츠

💾 static/hello-static.html

<!doctype html>
<html>
<head>
    <meta charset="UTF-8" http-equiv="Content-Type" content="text/html" />
    <title>static content</title>
</head>
<body>
정적 컨텐츠입니다.
</body>
</html>

 

 

 

MVC와 템플릿 엔진

  • MVC: Model, View, Controller

 

💾 controller/HelloController.java

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";
    }
}

 

💾 template/hello-template.html

<!doctype 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 파라미터가 필요하다는 것을 알 수 있다.

2023-08-27 20:12:58.724 WARN 13528 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]

 

http://localhost:8080/hello-mvc?name=devje

 

파라미터를 넣어주면 정상적으로 화면이 뜨는 것을 확인할 수 있다.

아니면 메소드 파라미터에 `required=false`를 추가하면, name이 없어도 에러가 뜨지는 않는다.

(required는 default가 true이다.)

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

대신 `null`로 들어온다.

 

 

 

API

💾 controller/HelloController.java

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(value = "name", required = false) String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }

    💡💡💡
    @GetMapping("hello-string")
    @ResponseBody // http body부에 데이터를 직접 넣어주겠다
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name; // hello spring
    }
}

 

API 방식
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(value = "name", required = false) String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }

    @GetMapping("hello-string")
    @ResponseBody // http body부에 데이터를 직접 넣어주겠다
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name; // hello spring
    }

    💡💡💡
    @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 사용 원리

  • `@ReponseBody`를 사용
    • HTTP BODY에 문자 내용을 직접 반환
    • `viewResolver` 대신에 `HttpMessageConverter`가 동작
    • 기본 문자처리: `StringHttpMessageConverter`
    • 기본 객체처리: `MappingJackson2HttpMessageConverter`
    • byte 처리 등등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있음

 

 

인프런 김영한 - <스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술> 실습
300x250