Spring/Spring-Basic

[스프링 입문] Section2. 스프링 웹 개발 기초

y-seo 2023. 3. 29. 18:30

정적 컨텐츠

  • 파일을 그대로 웹 브라우저에 전달
  1. 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와 템플릿 엔진

  • MVC : Model, View, Controller
    • Model : 화면에 필요한 것만 담
    • View : 화면을 그리는 데에 집중
    • Controller : 비즈니스 로직 같은 내부적인 것에 집중
  1. HelloController.java에 추가
    • @RequestParam : 외부에서 파라미터를 받을 것
@GetMapping("hello-mvc")
 public String helloMvc(@RequestParam("name") String name, Model model) {
 model.addAttribute("name", name);
 return "hello-template";
 }

2. resources/templates/hello-template.html 생성

  • thymleaf 템플릿 : 서버 없이도 파일의 absolute path로 접속 시 껍데기를 볼 수 있음 (hello! empty가 뜸)
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

  • 구동 방식
    • 변환하여 웹 브라우저에 반환하는 것이 포인트

 

API

1. HelloController.java에 추가

@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
    return "hello " + name;
}
  • @ResponseBody : http의 body부분에 데이터를 직접 넣어주겠다는 뜻, 요청한 클라이언트에 그대로 내려감, 템플릿 엔진과 달리 View가 없음 
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) { //객체 생성
    Hello hello = new Hello();
    hello.setName(name); //파라미터로 넘어온 name을 넣음
    return hello; //객체를 넘김
}
static class Hello {
    private String name; //key
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • Json 방식으로 데이터를 넘김
더보기

Json은 속성-값 쌍, 배열 자료형 또는 기타 모든 시리얼화 가능한 값 또는 "키-값 쌍"으로 이루어진 데이터 오브젝트를 전달하기 위해 인간이 읽을 수 있는 텍스트를 사용하는 개방형 표준 포맷이다.

요즘 추세는 JSON 방식, Spring도 기본이 JSON 방식으로 반환하는 것이다.

  • @ResponseBody 사용 원리

  • HTTP의 BODY에 문자 내용을 직접 반환
  • viewResolver 대신에 HttpMessageConverter 가 동작
  • 기본 문자처리: StringHttpMessageConverter
  • 기본 객체처리: MappingJackson2HttpMessageConverter (객체를 Json으로 바)
  • byte 처리 등등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있음
  • 객체가 오면 Json 방식으로 데이터를 만들어 Http 응답에 반환하겠다는 것이 기본 정책