[SB] 0. 스프링부트 맛보기

최재원's avatar
Mar 13, 2025
[SB] 0. 스프링부트 맛보기

First

1. 프로젝트 생성

notion image
notion image
notion image

2. 실행 해보기

notion image
notion image
notion image
  • 응답 해줄 파일이나 코드가 없어서 404 에러 발생

3. 코드 작성 후 실행 해보기

1. 폴더 구조 확인

notion image

2. UserRepository

package org.example.first.repository; // 책임 : 데이터 관리자 (DB, File System, 외부 서버) public class UserRepository { public String getData(){ return "user 1"; } public String getDataAll(){ return "user 1, user 2, user 3, user 4, user 5"; } }

3. UserController

package org.example.first.controller; import org.example.first.repository.UserRepository; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; // 책임 : 외부 클라이언트 요청 받기, 응답 하기 // RestController : return 되는 '값'을 버퍼에 담아 보냄 @RestController public class UserController { UserRepository userRepository = new UserRepository(); // GET 요청 (URL 브라우저 적기, 하이퍼링크) // http://localhost:8080/user/1 @GetMapping("/user/1") public String getData(){ return userRepository.getData(); } @GetMapping("/user") public String getDataAll(){ return userRepository.getDataAll(); } }
  • @RestController : return의 ‘값’을 버퍼에 담아 보냄

4. 서버 실행 후 요청 해보기

notion image
  • 실행은 이 클래스를 실행한다.
notion image
notion image
 

Second

1. 프로젝트 생성

notion image
notion image

폴더 구조

notion image

html파일 → mustache 파일로 변경

  • 템플릿을 사용하기 위해 변경
notion image
notion image
템플릿 엔진
html에 java코드를 넣을 수 있는 기술

코드 작성

model

package org.example.second.model; import lombok.AllArgsConstructor; import lombok.Getter; @Getter // 자동 get() 만들어주는 주석 @AllArgsConstructor // 자동 생성자 만들어주는 주석 public class User { private int id; private String username; private String email; }
  • @Getter : 자동 get() 만들어주는 주석
  • @AllArgsConstructor : 자동 생성자 만들어주는 주석

repository

package org.example.second.repository; import org.example.second.model.User; public class UserRepository { public User findOne(){ // select * from user_tb where id = 1; return new User(1,"ssar","ssar@nate.com"); } public User findTwo(){ // select * from user_tb where id = 2; return new User(2,"cos","cos@nate.com"); } }

controller

package org.example.second.controller; import org.example.second.model.User; import org.example.second.repository.UserRepository; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; // return 되는 값의 경로를 찾아서(templates -> *.mustache)을 찾아서 '파일'을 버퍼에 담아 보냄 @Controller public class UserController { UserRepository userRepository = new UserRepository(); @GetMapping("/user/1") public String findOne(Model model) { User user = userRepository.findTwo(); model.addAttribute("model", user); return "user/info"; }; @GetMapping("/v2/user/1") public @ResponseBody String findOneV2(){ User user = userRepository.findOne(); return """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>info page</h1> <hr> <div> ${user.id}, ${user.username}, ${user.email} </div> </body> </html> """ .replace("${user.id}",user.getId()+"") .replace("${user.username}", user.getUsername()) .replace("${user.email}", user.getEmail()); }; }
  • @Controller : return이 되는 파일을 찾아서 그 파일을 버퍼에 담아 보냄
    • 파일을 찾을 때의 경로는 정해져 있다.
    • templates폴더를 root경로로 한다
      • notion image
  • 기본 버전과 v2 버전의 차이
    • 기본 버전
      • @GetMapping("/user/1") public String findOne(Model model) { User user = userRepository.findTwo(); model.addAttribute("model", user); return "user/info"; };
        <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>info page</h1> <hr> <div> {{model.id}}, {{model.username}}, {{model.email}} </div> </body> </html>
      • html코드에 자바의 코드를 주입
    • v2 버전
      • @GetMapping("/v2/user/1") public @ResponseBody String findOneV2() { User user = userRepository.findOne(); return """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>info page</h1> <hr> <div> ${user.id}, ${user.username}, ${user.email} </div> </body> </html> """ .replace("${user.id}",user.getId()+"") .replace("${user.username}", user.getUsername()) .replace("${user.email}", user.getEmail()); };
      • 자바 코드에 html을 작성
 
Share article

jjack1