1. 프로젝트 생성



2. 서버 오류 (페이지를 찾을 수 없음)

3. 코드 작성
@RestController :
리턴 받은 값을 바로 버퍼에 돌려주는 것 (모든 메서드가 JSON 형태의 데이터로 반환)
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";
}
}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 getDate(){
return userRepository.getData();
}
// GET 요청 : url 브라우저 적기, 하이퍼링크
// http:localhost:8080/user
@GetMapping("/user")
public String getDateAll(){
return userRepository.getDataAll();
}
}Share article