
회원가입, 로그인 기능과 특정권한을 부여하는 백엔드 환경을 구성해보려고 한다.
인텔리제이 에서의 스프링부트 프로젝트 생성
디펜던시 추가, Web 에서 일단 2개만 선택 (나머지는 차차 알아가자)
Spring Web

이제 새로만든 애플리케이션을 실행해보자.
Thymelef 라는 템플릿 엔진으로 HTML 파일을 만들어 브라우저에서 확인해 보자
인텔리제이 공식홈 타임리프 소개
공식홈
pom.xml 파일에 추가한다.<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
clean -> compile
아래와 같은 경로에 TestController 라는 java class 파일을 만든다.
다음과 같이 작성한다.
package com.example.playground;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
@Controller
public class TestController {
@RequestMapping("/home") // 브라우저 주소창에 /home 이라는 path 접근을 요청
public String test(Model model) {
model.addAttribute("message", "Hello World");
model.addAttribute("items", Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4"));
return "start_page"; // start_page.html 에 위에 내용을 반환한다.
}
}
addAttribute 파라미터 값을 변수처럼 사용하고 있다.<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Title</title>
</head>
<body>
<div>
<h1 th:text="${message}"></h1>
<p>Thymeleaf Iteration:</p>
<ul>
<li th:each="item: ${items}" th:text="${item}"></li>
</ul>
<p th:text="'Current Year: ' + ${#dates.format(#dates.createNow(), 'yyyy')}"></p>
</div>
</body>
</html>
Run Ctrl + R
http://localhost:8080/home 접속백엔드 API 를 호출하듯이 단순 String 문자 출력해보기
원리 : @RestController 로 JSON 형태의 객체 데이터를 반환한다.
@RestController는 @Controller에 @ResponseBody가 추가된 것
출처: https://mangkyu.tistory.com/49 [MangKyu's Diary:티스토리]
java calss 작성
package com.example.playground;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class RestTestController {
@RequestMapping(value = "/rest", method = RequestMethod.GET)
public String rest() {
return "Hello World";
}
}
http://localhost:8080/rest 접속