반응형
이 글은 이지업 - 포토그램 만들기 수업을 토대로 작성하였습니다.
http 요청 과정을 간단하게 그려보면 아래와 같다.
클라이언트가 웹 서버로 요청을 하면 요청을 받은 웹 서버는 DB에서 정보를 가져오고 그것으로 응답하는 것이다.
클라이언트가 웹서버에 요청하는 방식은 4가지 방식이 있다.
(1) GET(동사) - 데이터 요청
(2) POST(동사) - 데이터 전송 (http body 필요)
(3) PUT(동사) - 데이터 갱신 (http body 필요)
(4) DELETE(동사) - 데이터 삭제
controller-demo 프로젝트 생성해서 확인해보기
package com.cos.controllerdemo.web;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
//@Controller // file을 응답하는 컨트롤러. (클라이언트가 브라우저면 .html 파일을)
@RestController // Data를 응답하는 컨트롤러. (클라이언트가 핸드폰이면 data)
public class HttpController {
// http://localhost:8080/get
@GetMapping("/get")
public String get() {
return "<h1>get요청됨.</h1>";
}
// http://localhost:8080/post
@PostMapping("/post")
public String post() {
return "post요청됨.";
}
// http://localhost:8080/put
@PutMapping("/put")
public String put() {
return "put요청됨.";
}
// http://localhost:8080/delete
@DeleteMapping("/delete")
public String delete() {
return "delete요청됨.";
}
}
그럼 각각의 요청들의 결과를 확인해보자.
- GetMapping
주소창에
localhost:8080/get
을 작성해보면 아래와 같이 결과를 확인할 수 있다.
(get만 웹브라우저에서 요청이 가능하고 나머진 불가능하다.)
나머지를 요청했을 경우 아래와 같은 화면이 나온다.
나머지 요청들을 확인하기 위해서는 기존에 설치했던 'Postman'을 이용하면 된다.
'Postman'을 사용하여 요청해보면 아래와 같은 화면이 나온다.
Post 요청
Put 요청
Delete 요청
반응형
'Programming > Spring' 카테고리의 다른 글
SPRING과 SPRING BOOT의 차이점 (0) | 2021.08.01 |
---|---|
[spring boot] http 요청 file로 응답 (0) | 2021.06.15 |
[spring boot] json 응답하기 (0) | 2021.06.08 |
[spring boot] http body 데이터 전송 (0) | 2021.06.08 |
[spring boot] controller - 쿼리스트링, 주소변수매핑 (0) | 2021.06.07 |