Back-End/Java Spring Boot

[Spring Boot] Spring Static Content

Hoplin 2022. 3. 12. 22:41
반응형

정적 컨텐츠란 서버에서 따로 프로그래밍적 처리 없이 파일을 바로 클라이언트에 전달하는 방식을 의미한다. 예를들면 Spring Welcome Page를 예로 들 수 있다. 스프링 Static Content 관련 Document를 살펴보자

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content

 

Spring Boot Reference Documentation

This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe

docs.spring.io

위 문장을 보면 스프링 부트는 static content를 class 경로 혹은 ServletContext의 /static( 혹은 /public, /respurces, / META-INF/resources 중 하나)에서 불러온다고 되어있다. 디폴트 프로젝트 경로에서 /static경로는 class path의 /resources 경로 하위에 있는 /static 디렉토리를 의미한다. 예를 들어 아래와 같이 static 디렉토리에 hello-static.html파일을 만들었다고 가정하자.  

만든 후 서버를 작동시킨다. 디폴트 localhost에 /hello-static.html 을 입력해서 들어가면 아래와 같이 작성한 html페이지가 나오는것을 볼 수 있다.

Spring에서 static content를 띄우는 원리는 아래와 같은 원리이다.

기본적으로 요청을 하면 내장 Tomcat 서버에서 request를 받는다. 그 다음 Spring Container에 요청 url parameter와 동일한 값으로 매핑된 Controller가 존재하는지 살펴본다. 만약 존재하지 않는경우, static 디렉토리를 검사하여, parameter와 동일한 이름의 html이 있는지 확인한다. 있다면, 해당 정적 파일(html)을 반환하게 되는것이다.

반응형