개발새발/Spring

220210_스프링입문_스프링 부트 시작하기_GET API(1)_패스트캠퍼스 챌린지 18일차

막동이아빠 2022. 2. 10. 22:46

220210_스프링입문_스프링 부트 시작하기_GET API(1)_패스트캠퍼스 챌린지 18일차

<2022년 02월 10일 _ 패스트캠퍼스 챌린지 18일차>

[스프링입문_스프링 부트 시작하기_GET API(1)]

 

1. 메소드의 특성

  의미 CRUD 멱등성 안정성 Path Variable Query
Parameter
DataBody
GET 리소스 취득 R O O O O X
POST 리소스 생성,추가 C X X O O
PUT 리소스 갱신, 생성 C / U O X O O
DELETE 리소스 삭제 D O X O O X
HEAD 헤더 데이터 취득 - O O - - -
OPTIONS 지원하는 메소드 취득 - O - - - -
TRACE 요청메시지 반환 - O - - - -
CONNECT 프록시 동작의 터널 접속으로 변경 - X - - - -

2. 실습

 (1) controller 패키지에 GetApiController 클래스 추가

package com.example.hello.controller;

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

@RestController
@RequestMapping("/api/get")
public class GetApiController {

    @GetMapping(path = "/hello") // get http://localhost:9090/api/get/hello
    public String hello(){
        return "get Hello";
    }

    //@RequestMapping("/hi") // get / post / put / delete 모두 동작함
    @RequestMapping(path = "/hi", method = RequestMethod.GET)
    // 상기와 같이 지정하면 GET으로만 동작
    // get http://localhost:9090/api/get/hi
    public String hi(){
        return "hi";
    }
}

 (2) 서버를 실행하고, Talend API Tester에서 

 http://localhost:9090/api/get/hello, http://localhost:9090/api/get/hi 를 실행하면 다음과 같은 결과를 얻을 수 있다.

 (3) Path Variable 을 받아보자

package com.example.hello.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/get")
public class GetApiController {
    // http://localhost:9090/api/get/path-variable/{name}
    //{}변하는 부분
    //GetMapping 내부의 {name} 과 변수의 name 명이 일치해야함
    @GetMapping(path = "/path-variable/{name}")
    public String pathVariable(@PathVariable String name){
        System.out.println("PathVariable : " + name);
        return name;
    }
}

 (4) 서버를 다시 실행하고, Talend API로 테스트한다

http://localhost:9090/api/get/path-variable/작은막쥔이

 다음과 같은 결과가 나온다

(5) 변수명과 path의 변수 명을 다르게 설정하고자 할때는 다음 코드의 pathVariable2 메소드처럼 설정한다

package com.example.hello.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/get")
public class GetApiController {
    // http://localhost:9090/api/get/path-variable/{name}
    //{}변하는 부분
    //GetMapping 내부의 {name} 과 변수의 name 명이 일치해야함
    @GetMapping(path = "/path-variable/{name}")
    public String pathVariable(@PathVariable String name){
        System.out.println("PathVariable : " + name);
        return name;
    }

    // http://localhost:9090/api/get/path-variable/{name}
    //{}변하는 부분
    //GetMapping 내부의 {name} 과 변수의 name 명이 일치해야함
    @GetMapping(path = "/path-variable2/{name}")
    public String pathVariable2(@PathVariable(name = "name") String pathName){
        System.out.println("PathVariable : " + pathName);
        return pathName;
    }
}

 (6) 서버를 다시 실행하고, Talend API로 테스트한다

http://localhost:9090/api/get/path-variable/큰막쥔이

 다음과 같은 결과가 나온다

 (7) Query Parameter 받아보기 는 다음 강의에서 이어집니다~

 

18일차 강의 완료~

필기 & 내 화면 스크린샷 저장

 

 

 

https://bit.ly/37BpXiC

 

패스트캠퍼스 [직장인 실무교육]

프로그래밍, 영상편집, UX/UI, 마케팅, 데이터 분석, 엑셀강의, The RED, 국비지원, 기업교육, 서비스 제공.

fastcampus.co.kr

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.