Search

Request

Request

: HTTP Request(์š”์ฒญ)๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋Š” ์š”์ฒญ ๋ฉ”์‹œ์ง€๋กœ, ์›น ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ๋™์ž‘(GET, POST, ๋“ฑ) ๋ฐ ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์™€ ํ•จ๊ป˜ ์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

Overview

โ€ข
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•
โ—ฆ
๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
โ—ฆ
API ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ ๋„๊ตฌ๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
โ–ช
Postman
โ–ช
Thunder Client
โ€ข
Thunder Client ๋กœ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ
โ€ข
์Šคํ”„๋ง ๋ถ€ํŠธ๋กœ ์„œ๋ฒ„์—์„œ ์š”์ฒญ ๋ฐ›์•„๋ณด๊ธฐ
โ—ฆ
์š”์ฒญ ํ…Œ์ŠคํŠธ
โ–ช
Thunder Client ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ
โ–ช
HTML form ํƒœ๊ทธ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•

โ€ข
๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
โ€ข
API ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ ๋„๊ตฌ๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

โ€ข
์ฃผ์†Œ ํ‘œ์‹œ์ค„์— URL ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์š”์ฒญํ•˜๊ธฐ
โ€ข
FORM ์ „์†ก ์š”์ฒญ ํ•˜๊ธฐ

์ฃผ์†Œ ํ‘œ์‹œ์ค„์— URL์„ ์ž…๋ ฅํ•˜์—ฌ ์š”์ฒญํ•˜๊ธฐ:

โ€ข
HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ: ์ผ๋ฐ˜์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฃผ์†Œ ํ‘œ์‹œ์ค„์— URL์„ ์ž…๋ ฅํ•˜์—ฌ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” HTTP ๋ฉ”์„œ๋“œ๋Š” GET์ž…๋‹ˆ๋‹ค.
โ€ข
ํŠน์ง•:
โ—ฆ
GET ๋ฉ”์„œ๋“œ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ณ  ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
โ—ฆ
์ฃผ์†Œ ํ‘œ์‹œ์ค„์— ์ง์ ‘ ์ž…๋ ฅํ•˜๋ฏ€๋กœ, ๋ธŒ๋ผ์šฐ์ €์— ์˜ํ•ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ—ฆ
์ฃผ๋กœ ์›นํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, URL์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

FORM ์ „์†ก ์š”์ฒญํ•˜๊ธฐ:

โ€ข
HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ: FORM ์ „์†ก์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋Š” GET ๋˜๋Š” POST์ž…๋‹ˆ๋‹ค.
โ€ข
ํŠน์ง•:
โ—ฆ
GET: ํผ ๋ฐ์ดํ„ฐ๋ฅผ URL์˜ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์— ํฌํ•จํ•˜์—ฌ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์ด๋‚˜ ์กฐํšŒ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ๋˜๋ฉฐ ๊ธธ์ด์— ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
โ—ฆ
POST: ํผ ๋ฐ์ดํ„ฐ๋ฅผ HTTP ์š”์ฒญ์˜ ๋ณธ๋ฌธ(body)์— ํฌํ•จํ•˜์—ฌ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ธธ์ด์— ์ œ์•ฝ์ด ์—†๊ณ , ๋ฏผ๊ฐํ•œ ์ •๋ณด ์ „์†ก์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•ˆ์ „ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
<form action="/board/insert" method="POST" id="form"> <input type="text" name="title" placeholder="์ œ๋ชฉ"> <input type="text" name="writer" placeholder="์ž‘์„ฑ์ž"> <textarea name="content" cols="40" rows="5" placeholder="๋‚ด์šฉ"></textarea> </form>
HTML
๋ณต์‚ฌ

GET vs POST

GET

์šฉ๋„
โ€ข
์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‚ฌ์šฉ๋จ.
โ€ข
์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ.
ํŠน์ง•
โ€ข
URL์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ
โ€ข
๊ธธ์ด์— ์ œํ•œ์ด ์žˆ์–ด ํ•œ์ •๋œ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์— ์ ํ•ฉ.
โ€ข
๋ธŒ๋ผ์šฐ์ €์—์„œ ์บ์‹ฑ๋  ์ˆ˜ ์žˆ์–ด, ๋ฐ˜๋ณต ์š”์ฒญ์— ์œ ์šฉํ•จ.

POST

์šฉ๋„
โ€ข
์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋จ.
โ€ข
์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉ.
ํŠน์ง•
โ€ข
๋ฐ์ดํ„ฐ๋ฅผ HTTP ์š”์ฒญ์˜ ๋ณธ๋ฌธ(body)์— ํฌํ•จํ•˜์—ฌ ์ „๋‹ฌ (๋ฐ์ดํ„ฐ๊ฐ€ URL์— ๋…ธ์ถœ๋˜์ง€ ์•Š์Œ).
โ€ข
๋ฐ์ดํ„ฐ ๊ธธ์ด์— ์ œํ•œ์ด ์—†์–ด ๋” ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์— ์ ํ•ฉ.
โ€ข
๋ฏผ๊ฐํ•œ ์ •๋ณด ์ „์†ก์— ์ ํ•ฉํ•˜๋ฉฐ, ๋ณด์•ˆ์„ ์œ„ํ•œ HTTPS ์‚ฌ์šฉ ๊ถŒ์žฅ.

API ๊ฐœ๋ฐœ ํ…Œ์ŠคํŠธ ๋„๊ตฌ๋ฅผ ํ†ตํ•ด ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

API ๊ฐœ๋ฐœ ํ…Œ์ŠคํŠธ ๋„๊ตฌ

: ๊ฐœ๋ฐœ์ž๊ฐ€ API๋ฅผ ๋งŒ๋“ค๊ณ , ํ…Œ์ŠคํŠธํ•˜๋ฉฐ, ๋””๋ฒ„๊น…ํ•˜๋Š”๋ฐ ๋„์›€์„ ์ฃผ๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

์ข…๋ฅ˜

โ€ข
Postman
โ€ข
Thunder Client (VSCODE)

Postman

โ€ข
API ๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ ๋ฐ ๋ฌธ์„œํ™”์— ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ํ”Œ๋žซํผ.
โ€ข
์‚ฌ์šฉ์ž ์นœํ™”์  ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ API ํ…Œ์ŠคํŠธ ๋ฐ ๋””๋ฒ„๊น…์ด ์šฉ์ดํ•จ.

Thunder Client (VSCODE)

๊ฒฝ๋Ÿ‰ Rest API ํด๋ผ์ด์–ธํŠธ ์ต์Šคํ…์…˜์œผ๋กœ, Visual Studio Code์šฉ์œผ๋กœ ์„ค๊ณ„๋œ ๊ฐ„๋‹จํ•˜๊ณ  ๊น”๋”ํ•œ ๋””์ž์ธ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Postman ์œผ๋กœ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ

1.
New
2.
HTTP Request
3.
Request ์ž‘์„ฑ
a.
์š”์ฒญ ๋ฉ”์†Œ๋“œ : GET
4.
Send

New

HTTP Request

Request ์ž‘์„ฑ

1.
์š”์ฒญ ๋ฉ”์†Œ๋“œ : GET

Send

์‘๋‹ต ๋ฉ”์‹œ์ง€

{ "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Host": "httpbin.org", "Postman-Token": "86f7879d-21ba-425e-a061-4d640b5dd900", "User-Agent": "PostmanRuntime/7.31.1", "X-Amzn-Trace-Id": "Root=1-6540f7d5-3e6af9f251d571d0093f073f" }, "origin": "114.201.106.211", "url": "https://httpbin.org/get" }
JSON
๋ณต์‚ฌ

Thunder Client ๋กœ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ

1.
VS CODE ์‹คํ–‰
2.
ํ™•์žฅ: ๋งˆ์ผ“ํ”Œ๋ ˆ์ด์Šค (Extension)
3.
Thnder Client ๊ฒ€์ƒ‰
4.
์„ค์น˜
5.
Thnder Client ์•„์ด์ฝ˜ ํด๋ฆญ
6.
New Request
7.
Request ์ž‘์„ฑ
a.
์š”์ฒญ ๋ฉ”์†Œ๋“œ : GET
8.
Send (์š”์ฒญ-Requset)
9.
๊ฒฐ๊ณผ ํ™•์ธ (์‘๋‹ต-Response)

๋” ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•

์„ค์น˜

โ€ข
VS CODE ์‹คํ–‰
โ€ข
ํ™•์žฅ: ๋งˆ์ผ“ํ”Œ๋ ˆ์ด์Šค (Extension)
โ€ข
Thunder Client ๊ฒ€์ƒ‰

Thunder Client ์•„์ด์ฝ˜ ํด๋ฆญ

New Request

Request ์ž‘์„ฑ

1.
์š”์ฒญ ๋ฉ”์†Œ๋“œ : GET

Send

์‘๋‹ต ํ™•์ธ

์Šคํ”„๋ง ๋ถ€ํŠธ๋กœ ์„œ๋ฒ„์—์„œ ์š”์ฒญ ๋ฐ›์•„๋ณด๊ธฐ

โ€ข
ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ
โ€ข
ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ
โ€ข
์ž‘์—… ํŒŒ์ผ
โ€ข
์‹คํ–‰ ๊ฒฐ๊ณผ
โ—ฆ
์š”์ฒญ ํ…Œ์ŠคํŠธ
โ–ช
GET
โ–ช
POST
โ–ช
PUT
โ–ช
DELETE

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

spring-request/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ main/ โ”‚ โ”‚ โ”œโ”€โ”€ java/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ com/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ joeun/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ springrequest/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ controller/ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ BoardController.java
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ dto/ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Board.java โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ServletInitializer.java โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ SpringRequestApplication.java โ”‚ โ”œโ”€โ”€ resources/ โ”‚ โ”‚ โ”œโ”€โ”€ application.properties โ”‚ โ”‚ โ”œโ”€โ”€ static/ โ”‚ โ”‚ โ””โ”€โ”€ templates/ โ””โ”€โ”€ build.gradle

ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ

โ€ข
Spring boot version : 3.3.5
โ€ข
Group Id : com.aloha
โ€ข
Artifact Id : spring-request
โ€ข
Java version : JDK 17
โ€ข
Packaging : WAR
โ€ข
Dependency
โ—ฆ
Spring Web
โ—ฆ
Spring Dev Tools
โ—ฆ
Lombok

์ž‘์—… ํŒŒ์ผ

โ€ข
build.gradle
โ€ข
Board.java
โ€ข
BoardController.java
โ€ข
build.gradle
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
SQL
๋ณต์‚ฌ
โ€ข
Board.java
/** * Board * - ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด */ @Data @NoArgsConstructor public class Board { private int boardNo; private String title; private String writer; private String content; private Date regDate; private Date updDate; private int views; public Board(String title, String writer, String content) { this.title = title; this.writer = writer; this.content = content; } }
Java
๋ณต์‚ฌ
โ€ข
BoardController.java
/** * ๊ฒŒ์‹œํŒ ์ปจํŠธ๋กค๋Ÿฌ * - ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก - [GET] - /board * - ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ - [GET] - /board/10 * - ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์ฒ˜๋ฆฌ - [POST] - /board * - ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์ฒ˜๋ฆฌ - [PUT] - /board * - ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ - [DELETE] - /board */ @Slf4j // ๋กœ๊ทธ ์‚ฌ์šฉ ์–ด๋…ธํ…Œ์ด์…˜ // @Controller // ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ๋กœ ๋นˆ๋“ฑ๋ก @RestController // @ResponseBody+@Controller : ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ REST API ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋นˆ๋“ฑ๋ก @RequestMapping("/board") public class BoardController { /** * ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก * [GET] * /board * - response : boardList * @return * @throws Exception */ @GetMapping(value="") public ResponseEntity<List<Board>> list() throws Exception { log.info("[GET] - /board"); List<Board> boardList = new ArrayList<>(); boardList.add(new Board("์ œ๋ชฉ1", "์ž‘์„ฑ์ž1", "๋‚ด์šฉ1")); boardList.add(new Board("์ œ๋ชฉ2", "์ž‘์„ฑ์ž2", "๋‚ด์šฉ2")); boardList.add(new Board("์ œ๋ชฉ3", "์ž‘์„ฑ์ž3", "๋‚ด์šฉ3")); return new ResponseEntity<>(boardList, HttpStatus.OK); } /** * ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ * [GET] * /board * - response : board * @param model * @param boardNo * @return * @throws Exception */ @GetMapping(value="/{boardNo}") public ResponseEntity<Board> read(@PathVariable("boardNo") int boardNo) throws Exception { log.info("[GET] - /board/read"); Board board = new Board("์ œ๋ชฉ1", "์ž‘์„ฑ์ž1", "๋‚ด์šฉ1"); return new ResponseEntity<>(board, HttpStatus.OK); } /** * ๊ฒŒ์‹œ๊ธ€ ์“ฐ๊ธฐ ์ฒ˜๋ฆฌ * [POST] * /board * - response : * โœ… ์š”์ฒญ ์„ฑ๊ณต : 200 OK "SUCCESS" * โŒ ์š”์ฒญ ์‹คํŒจ : 400 Bad Request "FAIL" * @param board * @return * @throws Exception */ @PostMapping(value="") public ResponseEntity<String> insertPro(Board board) throws Exception { int result = new Random().nextInt(2); if( result == 0 ) { return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST); } return new ResponseEntity<>("SUCCESS", HttpStatus.OK); } /** * ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์ฒ˜๋ฆฌ * [PUT] * /board * - response : * โœ… ์š”์ฒญ ์„ฑ๊ณต : 200 OK "SUCCESS" * โŒ ์š”์ฒญ ์‹คํŒจ : 400 Bad Request "FAIL" * @param board * @return * @throws Exception */ @PutMapping(value="") public ResponseEntity<String> updatePro(Board board) throws Exception { int result = new Random().nextInt(2); if( result == 0 ) { return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST); } return new ResponseEntity<>("SUCCESS", HttpStatus.OK); } /** * ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ * [DELETE] * /board * - response : * โœ… ์š”์ฒญ ์„ฑ๊ณต : 200 OK "SUCCESS" * โŒ ์š”์ฒญ ์‹คํŒจ : 400 Bad Request "FAIL" * @param boardNo * @return * @throws Exception */ @DeleteMapping(value="") public ResponseEntity<String> deletePro(@RequestParam("boardNo") int boardNo) throws Exception { log.info("boardNo : " + boardNo); int result = new Random().nextInt(2); if( result == 0 ) { return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST); } return new ResponseEntity<>("SUCCESS", HttpStatus.OK); } }
Java
๋ณต์‚ฌ

์‹คํ–‰ ๊ฒฐ๊ณผ

์š”์ฒญ ํ…Œ์ŠคํŠธ

โ€ข
Thunder Client ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ
โ€ข
HTML form ํƒœ๊ทธ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ
โ€ข
โ—ฆ
Body Form title, writer, content
โ€ข
โ—ฆ
Body Form boardNo, title, writer, content

Thunder Client ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ

โ€ข
Body Form title, writer, content
โ€ข
Body Form boardNo, title, writer, content

Run Collection

HTML form ํƒœ๊ทธ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ

โ€ข
build.gradle
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
Plain Text
๋ณต์‚ฌ
build.gradle ์ˆ˜์ • ํ›„, [ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ] > [ํ”„๋กœ์ ํŠธ ๋‹ค์‹œ ๋นŒ๋“œ] ํ•ด์•ผ ํ”„๋กœ์ ํŠธ ๋‹ค์‹œ ๋นŒ๋“œ
โ€ข
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML form ํƒœ๊ทธ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ</title> </head> <body> <h1>HTML form ํƒœ๊ทธ๋กœ ์š”์ฒญ ํ…Œ์ŠคํŠธ</h1> <p> POST, PUT, DELETE ๋Š” ์‹ค์ œ DB ์™€ ์—ฐ๊ฒฐ์ด ์•ˆ ๋˜์–ด ์žˆ๊ณ , <br> ๋žœ๋ค์œผ๋กœ SUCCESS, FAIL ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค. </p> <hr> <ul> <li>GET : <a href="http://localhost:8080/board">http://localhost:8080/board</a></li> <li>GET : <a href="http://localhost:8080/board/10">http://localhost:8080/board/10</a></li> <li> POST : <a href="http://localhost:8080/board">http://localhost:8080/board</a> <ul> <li>Body โžก Form โžก title, writer, content</li> </ul> </li> <li> PUT : <a href="http://localhost:8080/board">http://localhost:8080/board</a> <ul> <li>Body โžก Form โžก boardNo, title, writer, content</li> </ul> </li> <li> DELETE : <a href="http://localhost:8080/board">http://localhost:8080/board/10</a> <ul> <li>Body โžก Form โžก boardNo</li> </ul> </li> </ul> <hr> <h2>- GET : http://localhost:8080/board</h2> <form action="/board" method="GET"> <input type="submit" value="์š”์ฒญ"> </form> <hr> <h2>- GET : http://localhost:8080/board/10</h2> <form action="/board/10" method="GET"> <input type="submit" value="์š”์ฒญ"> </form> <hr> <h2>- POST : http://localhost:8080/board</h2> <h3>- Body โžก Form โžก title, writer, content</h3> <form action="/board" method="POST"> <input type="text" name="title" placeholder="title" value="์ œ๋ชฉ"> <br> <input type="text" name="writer" placeholder="writer" value="์ž‘์„ฑ์ž"> <br> <input type="text" name="content" placeholder="content" value="๋‚ด์šฉ"> <br> <input type="submit" value="์š”์ฒญ"> </form> <hr> <h2>- PUT : http://localhost:8080/board</h2> <h3>- Body โžก Form โžก boardNo, title, writer, content</h3> <!-- form ํƒœ๊ทธ์—์„œ๋Š” method: GET,POST ๋งŒ ์ง€์›๋จ --> <form action="/board" method="POST" id="form-put"> <input type="text" name="boardNo" placeholder="boardNo" value="10"> <br> <input type="text" name="title" placeholder="title" value="์ œ๋ชฉ"> <br> <input type="text" name="writer" placeholder="writer" value="์ž‘์„ฑ์ž"> <br> <input type="text" name="content" placeholder="content" value="๋‚ด์šฉ"> <br> <input type="button" value="์š”์ฒญ" onclick="update()"> </form> <hr> <h2>- DELETE : http://localhost:8080/board</h2> <h3>- Body โžก Form โžก boardNo</h3> <!-- form ํƒœ๊ทธ์—์„œ๋Š” method: GET,POST ๋งŒ ์ง€์›๋จ --> <form action="/board" method="POST" id="form-delete"> <input type="text" name="boardNo" placeholder="boardNo" value="10"> <br> <input type="button" value="์š”์ฒญ" onclick="remove()"> </form> <hr> <script> // ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์š”์ฒญ - JSON function update() { let request = new XMLHttpRequest() // ์ž…๋ ฅ ์ •๋ณด let form = document.getElementById('form-put') let boardNo = form.boardNo.value let title = form.title.value let writer = form.writer.value let content = form.content.value // JSON let data = { 'boardNo' : boardNo, 'title' : title, 'writer' : writer, 'content' : content } // ์‘๋‹ต ํ™•์ธ request.onreadystatechange = function() { // ์š”์ฒญ ์„ฑ๊ณต if( request.readyState == request.DONE && request.status == 200 ) { let response = request.responseText alert(response) } // ์š”์ฒญ ์‹คํŒจ if( request.readyState == request.DONE && request.status == 400 ) { let response = request.responseText alert(response) } // ์—๋Ÿฌ ๋ฐœ์ƒ if( request.readyState == request.DONE && request.status == 500 ) { alert('์„œ๋ฒ„์ธก ์—๋Ÿฌ ๋ฐœ์ƒ') } } // ์š”์ฒญ ์„ค์ • let url = 'http://localhost:8080/board' request.open("PUT", url, true) request.setRequestHeader("Content-Type", "application/json") // JSON.stringify() : JavaScript ์˜ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ request.send( JSON.stringify(data) ) } // ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์š”์ฒญ - JSON function remove() { let request = new XMLHttpRequest() // ์ž…๋ ฅ ์ •๋ณด let form = document.getElementById('form-put') let boardNo = form.boardNo.value // JSON let data = { 'boardNo' : boardNo, } // ์‘๋‹ต ํ™•์ธ request.onreadystatechange = function() { // ์š”์ฒญ ์„ฑ๊ณต if( request.readyState == request.DONE && request.status == 200 ) { let response = request.responseText alert(response) } // ์š”์ฒญ ์‹คํŒจ if( request.readyState == request.DONE && request.status == 400 ) { let response = request.responseText alert(response) } // ์—๋Ÿฌ ๋ฐœ์ƒ if( request.readyState == request.DONE && request.status == 500 ) { alert('์„œ๋ฒ„์ธก ์—๋Ÿฌ ๋ฐœ์ƒ') } } // ์š”์ฒญ ์„ค์ • let url = 'http://localhost:8080/board/' + boardNo request.open("DELETE", url, true) // JSON.stringify() : JavaScript ์˜ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ request.send() } </script> </body> </html>
HTML
๋ณต์‚ฌ

Github (์†Œ์Šค์ฝ”๋“œ)