IT recording...
[Spring] 스프링 입문(Static, Dynamic, API) 본문
1. 툴
- IntelliJ (무료버전) , Java 10.0.2
원래 Java 10.0.2 버전에 이클립스를 사용 중이었는데 강의에서 IntelliJ 와 Java 11버전을 사용하라고 권장해 주었다.
IntelliJ에는 많은 단축키들이 존재해서 사용하면 좋을 것 같아 설치했고, 자바 버전은 나중에 충돌 나는 부분이 생기면 업데이트 해주려 한다. (11다운받으러 갔더니 오라클 서버가 말썽부려서 못 받음)
- 스프링 프로젝트 기본 틀 생성해주는 사이트 (gradle같은 것들의 기본 설정을 해준다.)
2. 공식 문서
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
- 궁금한 부분이 있을 때 해당 사이트로 이동하여 영어로 읽는 것을 습관화 해두자.
3. Static(정적) / Dynamic(동적) / API
- static/index.html 은 WelcomePage 기능을 한다.
- 동작 흐름
웹 브라우저에서 주소를 넘겨주면 > TomCat 서버 거침 > Spring의 Controller에 해당 주소가 Mapping 되어 있는지 검사
STATIC -> (없으면) > static/*.html에서 찾고 > 웹 브라우저에 뿌리기
DYNAMIC -> (있으면) > template/*.html에서 찾고 > viewResolver로 데이터 등 처리해서 > html 변환 후 > 웹 브라우저에 뿌리기
API -> (있고 @ResponseBody 붙어있으면) > 객체 자체가 넘어와서 > json으로 변환 후 > HttpMessageConverter 동작
(API는 view가 존재하지 않는다.)
(api는 안드로이드에 서버 정보를 전달할 때, 서버와 서버끼리 통신할 때 주로 사용한다. )
- dynamic 예제
//실행 : http://localhost:8080/hello-mvc?name=spring
// controller/HelloController
Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}}
// resources/template/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
- API 예제
//실행 : http://localhost:8080/hello-api?name=spring
// controller/HelloController
//json 방식 key-value로 이루어진 구조로 나온다.
@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;
}
}
}
- MVC (model,view,controller)
'Spring' 카테고리의 다른 글
[Spring] GithubAction + S3 + CodeDeploy + NginX를 이용한 무중단 배포 (0) | 2022.01.13 |
---|---|
[Spring] 자동배포시 secret 관리 (0) | 2022.01.13 |
[Spring] Spring Security + Swagger2 연결 (0) | 2021.12.21 |
[Spring] 회원 관리 예제 (Controller, Service, Repository, Domain의 역할) + Singleton,DI,IoC/ Optional,Assertions / JUnit test (1) | 2021.03.08 |
[Spring] 프레임워크로 Spring을 선택하게 된 이유 (0) | 2021.03.08 |