[스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술] 01. 프로젝트 환경 설정

Turtle·2024년 6월 7일
0
post-thumbnail

💻️프로젝트 생성

https://start.spring.io/

스프링 부트 스타터 사이트로 이동해서 아래와 같이 스프링 프로젝트를 생성

❗SNAPSHOP(스냅샷)버전은 완벽하지 않으므로 선택X

압축을 푼 다음 인텔리제이 IDE로 오픈

main을 실행한 다음 URL에 http://localhost:8080/을 입력하여 결과 확인

❓라이브러리 살펴보기

스프링 부트 라이브러리

  • spring-boot-starter-web
    • spring-boot-starter-tomcat: 톰캣 (웹서버)
    • spring-webmvc: 스프링 웹 MVC
  • spring-boot-starter-thymeleaf: 타임리프 템플릿 엔진(View)
  • spring-boot-starter(공통): 스프링 부트 + 스프링 코어 + 로깅
  • spring-boot
    • spring-core
  • spring-boot-starter-logging
    • logback, slf4j

테스트 라이브러리

  • spring-boot-starter-test
    • junit: 테스트 프레임워크
    • mockito: 목 라이브러리
    • assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
    • spring-test: 스프링 통합 테스트 지원

📚️View 환경설정

출처 : 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는 논리 뷰 이름과 물리 뷰 이름 간 매핑 역할을 수행

🏃‍♂빌드하고 실행하기

  1. ./gradlew build
  2. cd build/libs
  3. java -jar hello-spring -0.0.1-SNAPSHOT.jar
  4. 실행 확인

$ 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의 자바 버전을 수정하고 다시 빌드하여 해결할 수 있었다.

🔒출처

스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

0개의 댓글