스프링 부트 스타터 사이트로 이동해서 아래와 같이 스프링 프로젝트를 생성
❗SNAPSHOP(스냅샷)버전은 완벽하지 않으므로 선택X
압축을 푼 다음 인텔리제이 IDE로 오픈
main
을 실행한 다음 URL에 http://localhost:8080/
을 입력하여 결과 확인
스프링 부트 라이브러리
테스트 라이브러리
출처 : Spring documentation(welcome pages)
Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.
스프링 부트에서는 정적 및 템플릿 welcome page를 지원한다. 처음에는 정적 컨텐츠 쪽에서 index.html
이란 이름의 파일을 찾는다. 만약 없다면 index
템플릿을 찾는다. 둘 중 하나를 찾으면 어플리케이션의 welcome page로 자동으로 사용된다.
package hello.hello_spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello");
return "hello";
}
}
Spring MVC can map incoming HTTP requests to handlers by looking at the request path and matching it to the mappings defined in your application (for example, @GetMapping annotations on Controller methods).
스프링 MVC는 요청 경로를 보고 이를 애플리케이션에 정의된 매핑(컨트롤러 메서드의 @GetMapping 어노테이션)과 일치시켜 들어오는 HTTP 요청을 핸들러에 매핑할 수 있다.
출처 : Spring documentation(Controller Return Value)
컨트롤러의 리턴 값으로 String
이 반환되는 경우 ViewResolver(뷰 리졸버) 구현으로 확인
ViewResolver는 논리 뷰 이름과 물리 뷰 이름 간 매핑 역할을 수행
./gradlew build
cd build/libs
java -jar hello-spring -0.0.1-SNAPSHOT.jar
$ java -jar hello-spring-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: hello/hello_spring/HelloSpringApplication has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtim
e only recognizes class file versions up to 61.0
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:524)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:427)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:421)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:712)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:420)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at org.springframework.boot.loader.net.protocol.jar.JarUrlClassLoader.loadClass(JarUrlClassLoader.java:103)
at org.springframework.boot.loader.launch.LaunchedClassLoader.loadClass(LaunchedClassLoader.java:91)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:88)
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53)
at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58)
위와 같은 에러를 마주했는데 찾아보니 자바 버전의 문제였고 build.gradle
의 자바 버전을 수정하고 다시 빌드하여 해결할 수 있었다.