inblog logo
|
sxias
    스프링부트

    [스프링부트] 3. 스프링부트 맛보기 1

    문정준's avatar
    문정준
    Mar 13, 2025
    [스프링부트] 3. 스프링부트 맛보기 1
    Contents
    1. New Project2. 서버 작동3. 코드 작성

    1. New Project

    • Spring Boot 체크
    • Name, Directory, Language, Type, Java Version, Packaging 체크
    notion image
    • Dependencies 다운로드
      • Spring Boot DevTools
      • Lombok
      • Spring Web
      • Mustache
    notion image
    • 우측의 Added Dependencies 목록 확인 후 Create
    notion image
     

    2. 서버 작동

    • src → main → java → org.example.first → FirstApplication 작동
      • ERROR가 뜰 경우 Apache Tomcat이 이미 실행 중일 경우가 높음 : 종료하기
      • INFO가 뜨면 정상 작동
    notion image
     
    package org.example.first; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FirstApplication { public static void main(String[] args) { SpringApplication.run(FirstApplication.class, args); } }
    notion image
     
    • 현재 자원 (Resource)이 없다는 메시지를 출력
    • 서버는 정상 가동됨을 확인할 수 있음
    notion image
     

    3. 코드 작성

    • UserController, UserRepository 생성 후 코드 작성
    notion image
    notion image
     
    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; // Role : 외부 클라이언트 요청 받기, 응답하기 @RestController public class UserController { UserRepository userRepository = new UserRepository(); // GET 요청 (URL 브라우저에 적기, 하이퍼링크) // http://localhost:8080/user/1 @GetMapping("/user/1") public String getData() { return userRepository.getData(); } // GET 요청 (URL 브라우저에 적기, 하이퍼링크) // http://localhost:8080/user/1 @GetMapping("/user") public String getDataAll() { return userRepository.getDataAll(); } }
     
    package org.example.first.repository; // Role : Data Manager (DB, FS, 외부 서버) public class UserRepository { public String getData(){ return "user 1"; } public String getDataAll(){ return "user 1, user 2, user 3, user 4, user 5"; } }
     

    결과

    • 주소 창에 localhost:8080/user 입력 시
    notion image
     
    • 주소 창에 localhost:8080/user/1 입력 시
    notion image
    Share article
    Contents
    1. New Project2. 서버 작동3. 코드 작성

    Sxias ㆍ a32176740@gmail.com

    RSS·Powered by Inblog