changki123's Foundation

Spring boot 3 helloworld

2024. 4. 3. 19:58 | JAVA/Spring


728x90

Spring boot 3.0

java 17 버전 (기준 && 최소사양)
java EE -> jakarta EE 9+ (javax.* 에서 jakarta.* 로 변경.) https://www.samsungsds.com/kr/insights/java_jakarta.html
Spring Boot 2.7.x 는 2023년 11월까지 지원 예정.
https://youtu.be/H6HwoWZtngs?si=IKqLl9sDphDmHo

 

JDK 17 download

https://www.oracle.com/kr/java/technologies/downloads/#jdk17-windows

 

IntelliJ IDEA Community Edition download

https://www.jetbrains.com/idea/download/?source=google&medium=cpc&campaign=APAC_en_KR_IDEA_Branded&term=intellij+idea&content=693444343130&gad_source=1&gclid=Cj0KCQjw5cOwBhCiARIsAJ5njuaNYG5R23pyleiMpqnH_RMeaVCNMrcmCGA4fRKAZmoJHBEXPP4mZcAaAvoXEALw_wcB&section=windows

 

Spring Initializr download

https://start.spring.io/

Generate 해서 파일 다운로드후 원하는 곳에서 intellij open

 

build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.4'
	id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	// default
	implementation 'org.springframework.boot:spring-boot-starter-web'

	// lombok
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	// Spring Boot Starter Thymeleaf 의존성
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

 

application.properties

# Server configuration
server.port=8080

# Thymeleaf 템플릿 엔진을 사용할 때 ViewResolver 설정
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

 

login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Login</title>
</head>
<body>
<h2>Login</h2>
<form th:action="@{/login}" method="post">
    <div><label> Username: <input type="text" name="username"/> </label></div>
    <div><label> Password: <input type="password" name="password"/> </label></div>
    <div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>

 

Application.class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

 

controller package 생성 -> 하위폴더에 Controller 생성

 

MainController.class

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class MainController {

    @GetMapping("/login")
    public String test(){
        return "login";
    }
}

 

MyErrorController.class

package com.example.demo.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError() {

        return "서비스에 문제가 발생했습니다. 관리자에게 문의하세요.";
    }

}

 

이제 실행을 하려면 Application.class 로 이동 후

 

모르겠으면 Run / Debug 아무거나 누르고

콘솔에 이상태로 있으면 springboot 가 가동중인 상태이므로

 

http://localhost:8080/login 으로 접속시 

아주 잘 뜨는걸 확인 할 수 있다.

728x90