[SpringBoot] SpringBoot with Gradle
1. SpringBoot with Gradle
- Java 11
- IDE : IntelliJ or Eclipse
다운 받은 뒤 lecture/springBootStudy 폴더 밑에 압축을 푼다.
IntelliJ 실행
버전은 2022.3(Community Edition)
open or import -> build.gradle 열기
No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.2 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
- No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.2 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
어쩌구 어쩌구 하면서 오류 발생. 아마 JDK 1.8 버전으로 세팅되어 있지 않나 싶음.
https://dev-emmababy.tistory.com/139
[IntelliJ] JAVA 버전 바꾸는 방법(JDK버전)
기존에 8버전을 쓰다가 11버전으로 변경해야 하는 프로젝트가 생겨서 기록해보는 "버전변경방법" 인텔리제이에서 6곳을 변경해주면 된다 (5곳 - 버전변경선택 / 1곳 - 원하는 JDK선택) Project Structure
dev-emmababy.tistory.com
여길 참고 해서 변경. 하지만 계속 뜸. 오류 문구에서 jar 어쩌구를 본것 같아서 jar로 다시 받아보기로함.
그래도 안되어서 Gradle 버전 2.7.8버전으로 변경해서 성공. 왜 3버전은 안되는지 모르겠지만 여튼 그럼.
되고 나니 바로 실행이 가능했다.
이때까지 한것 중 가장 빠른 환경설정.
template을 쓰지 않고 정적 렌더링을 할 경우 resources의 static 아래 HTML을 만든다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
</body>
</html>
이름은 index.html 왜냐면 규칙임. springboot 페이지에 들어가면 이 hello world 페이지 규칙에 대해 나와있음.
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
하지만 템플릿(동적 렌더링)을 쓴다면
resources아래의 templates에 폴더에 작성해 넣는다.
package hello.hellospring.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", "helloWorld!");
return "hello";
}
}
return에 들어가는 것은 HTML의 이름. 그러면 addAttribute에서 데이터 값에 해당하는 helloWorld! 를 전달해주게 된다.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Message is ' + ${data}" > Nice to meet you!</p>
</body>
</html>
Controller 단의 data에 해당하는 데이터가 Thymeleaf 문법으로 data에 들어가게 된다.
빌드
1. ./gradlew build
2. cd build/libs
3. java -jar ~.jar(빌드 된 결과물 jar 파일)
웹 개발 방식
1. 정적 컨텐츠
2. MVC와 템플릿 엔진
3. API 방식
MVC(@RequestParam(""))
API(@ResponseBody, @RquestParam(""))
-> return이 클래스일 경우 Json 방식으로 데이터를 내려줌.