Back-end/Spring-boot
Spring 웹 개발 기초
김병찬
2025. 2. 3. 15:43
정적 컨텐츠
- Spring-boot 정적 컨텐츠 기능 제공
- https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content
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와 Template Engine
- Model, View, Controller
- View 는 화면을 그리는 데 집중을 하는 것.
- Controller 는 서버 뒷단, 비즈니스 로직에 대해 집중함.
1️⃣ Controller
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-mvc";
}
}
- Option 단축키 : command + p
- required = True (default) 이기 때문에, 값을 무조건 넘겨야 함. -> ?name = " "
2️⃣ MVC, Template Engine 이미지
API
- command + shift + enter : Code completion
- Getter / Setter 단축키 : Command + N
Java Bean 규약
1. Java Bean 은 특정 패키지에 속해 있어야 한다.
2. 기본 생성자가 존재해야 한다.
-> Overloading 되지 않은 매개변수 값이 없는 기본 생성자가 존재해야 함.
Overloading 은 Method overloading 과 Constructor overloading 이 존재한다.
Method overloading 은 매개변수의 개수나 타입을 달리 하여 같은 이름의 메서드를 중복하여 정의하는 것.
Constructor overloading 은 매개변수를 달리 하여 생성자를 두 개 이상 구현하는 것.
3. 멤버변수의 접근제어자는 private 으로 선언되어야 한다.
4. 멤버변수에 접근 가능한 getter 와 setter 메서드가 존재해야 한다.
1️⃣ @ResponseBody 사용 원리
- HTTP Body에 문자 내용을 직접 반환
- viewResolver 대신에 HttpMessageConverter 가 동작
- 기본 문자처리 : StringHttpMessageConverter
- 기본 객체처리: MappingJackson2HttpMessageConverter
- byte 처리 등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있음