1. 정적 컨텐츠
스프링 부트 정적 컨텐츠 기능이 있다.
resources 폴더안에 static 폴더에 정적인 컨텐츠는 컨트롤러가 없어도 해당 URI가 자동으로생성된다.
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/static.html
<!DOCTYPE html>
<html lang="kr">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>정적 페이지 입니다. </p>
</body>
</html>
- 실행결과
URI = http://localhost:8090/static.html
2. MVC와 템플릿 엔진
Model / Controller / View
- Controller HelloController
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-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name",name);
return "hello-template";
}
}
- model hello-template.html
<!DOCTYPE HTML>
<!--타임리프 사용-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello! ' + ${name}" >Hello! </p>
</body>
</html>
- 실행결과
3. API
@ReponsBody 를 사용하여 전달 값을 전달
- HTTP의 BODY에 문자 내용을 직접 반환한다.
- viewResolver 대신 HttpMessageConverter 가 동작
- 기본 문자 처리 : StringHttpMessageConverter
- 기본 객체 처리 : MappingJackson2HttpMessageConverter
- byte 처리 등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있다.
- Controller 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;}
}
- 실행결과
객체 타입을 반환 해주어 > Json 형식의 데이터를 BODY에서 받고 있다.
'BackEnd > Spring Boot' 카테고리의 다른 글
[ Spring ] MySQL - (1) 이클립스 세팅 및 실행 (1) | 2023.02.28 |
---|---|
[ Spring ] xml - 스프링 프로젝트 실행 절차의 이해 (0) | 2023.02.26 |
[ Spring ] eGovframe 이클립스 설치 (0) | 2023.02.26 |
[ Spring / SpringBoot ] View 환경설정 (0) | 2023.02.24 |
[ Spring / SpringBoot ] 스프링부트 프로젝트 생성 및 실행 시켜보기 (0) | 2023.02.24 |