IT recording...

[Spring] 스프링 입문(Static, Dynamic, API) 본문

Spring

[Spring] 스프링 입문(Static, Dynamic, API)

I-one 2021. 3. 8. 12:11

1. 툴

- IntelliJ (무료버전) , Java 10.0.2

원래 Java 10.0.2 버전에 이클립스를 사용 중이었는데 강의에서 IntelliJ 와 Java 11버전을 사용하라고 권장해 주었다. 

IntelliJ에는 많은 단축키들이 존재해서 사용하면 좋을 것 같아 설치했고, 자바 버전은 나중에 충돌 나는 부분이 생기면 업데이트 해주려 한다. (11다운받으러 갔더니 오라클 서버가 말썽부려서 못 받음)

 

- 스프링 프로젝트 기본 틀 생성해주는 사이트 (gradle같은 것들의 기본 설정을 해준다.)

start.spring.io

2. 공식 문서

docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-welcome-page

 

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) 

 

Comments