관리 메뉴

나만의공간

Spring Boot Project Initializr (RestFul API 연결) #2 본문

IT/Spring

Spring Boot Project Initializr (RestFul API 연결) #2

밥알이 2022. 1. 1. 15:48

연재순서

1. Spring Boot Project Initializr (스프링 부트 프로젝트 생성법)
2. Spring Boot Project Initializr (RestFul API 연결)
3. Spring Boot Project Initialzr (Swagger 연결)  
4. Spring Boot Project Initializr (Log4j 연결)
5. Spring Boot Project Initializr (Requet Get/Post 출력)
6. 번외 : Spring Boot War 파일 생성, 로컬 Tomcat 뛰우기
7.  Srping Boot Project Initializr (GitHub 위치) 

RestFul API URL연결

Spring Boot init 파일을 이용하여 브라우저에서 404 페이지가 나오는거 까지 정상적으로 구동을 하였습니다.
이제 404 페이지가 아닌 내가 호출한 페이지에 결과값이 나오도록 추가 설정을 진행 합니다.

SpringBoot가 시작될때 URI를 가지고 있는 Controller를 인지 할수 있도록 Package를 아래 이미지 처럼 만듭니다.
com.study.studyTwo아래 controller Package를 만들고 그 밑에 StudyController Class를 작성 합니다.
(com.study.studyTwo Package아래가 다른 다른곳에 만들경우 자동 인식이 안됩니다.)
(보기쉬운 편의상 studyTwo라는 대문자를 만들었는데 Package명은 소문자로 만들어야 합니다. ^^)

controller Package아래 StudyController Class를 만들고 아래 소스를 넣습니다. 

package com.study.studyTwo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudyController {

    @GetMapping("/study")
    public String helloWorld() {
        return "Hello World Study";
    }

    @RequestMapping("/")
    public String helloWorldMain() {
        return "hello World Main";
    }
}

이제 서버를 재시작 합니다.

URL 접속 결과

메인 URI접속 화면

/study URI접속 화면

메인접속 URI를 localhost:8080/study 변경하고 싶으면 application.properties 파일에 Context위치를 아래와 같이 추가 하면 됩니다.

server.servlet.context-path=/study

다음편은 Swagger를 연결해 보겠습니다.

Comments