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

백하림's avatar
Mar 13, 2025
[스프링부트] 1. 스프링부트 맛보기(1)
💡
📌 RestController 언제 사용해야 할까?
  • REST API를 개발할 때
  • JSON 또는 일반 데이터를 반환해야 할 때
  • View(HTML 페이지) 렌더링이 필요 없을 때

1. 프로젝트 만들기

notion image
notion image

2. 코드 작성

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 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(); } }
package org.example.first.repository; // 책임 : 데이터 관리자 (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"; } }
notion image
notion image
Share article

harimmon