『스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술』
강의를 수강하며 개인 학습 목적으로 정리한 내용입니다.
스프링 웹 개발 기초
- 정적 컨텐츠
- MVC와 템플릿 엔진
- API - 데이터로 내리기
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>
동작 원리

2. MVC와 템플릿 엔진
- 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";
}
}
View
resources/templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
동작 원리

3. API - 데이터로 내리기
3-1. @ResponseBody - 문자 반환
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
}
- @ResponseBody 를 사용하면 뷰 리졸버(viewResolver )를 사용하지 않음
- 대신 HTTP의 BODY에 문자 내용을 직접 반환
3-2. @ResponseBody - 객체 반환
@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 를 사용하고, 객체를 반환하면 객체가 JSON으로 변환됨
동작 원리

@ResponseBody 를 사용
- HTTP의 body에 문자 내용을 직접 반환
- viewResolver 대신에 HttpMessageConverter 가 동작
- 문자처리 → StringHttpMessageConverter
- 객체처리 → MappingJackson2HttpMessageConverter
'Lecture > 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술' 카테고리의 다른 글
| 프로젝트 환경 설정(2) - Welcome page (0) | 2026.02.07 |
|---|---|
| 프로젝트 환경설정 - 프로젝트 생성, 라이브러리 살펴보기 (0) | 2026.02.06 |