Spring Boot ๐ŸŒผ - HTTP ์š”์ฒญ ์ฒ˜๋ฆฌ Annotation

Doridamยท2023๋…„ 6์›” 5์ผ
0

Spring๐ŸŒฑ & Boot ๐ŸŒผ

๋ชฉ๋ก ๋ณด๊ธฐ
10/12


HTTP ์š”์ฒญ ์ฒ˜๋ฆฌ Annotation

Spring์€ URL๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ Mapping๋œ URL์„ ์ฐพ์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ด๋•Œ URL์„ Mapping ํ•˜๋Š” Annotation์ด @RequestMapping๊ณผ ๊ทธ๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋œ @Get, @Post, @Put, @Update,.. Mapping์ด๋‹ค.
๊ธฐ์กด ํ˜•ํƒœ๋Š” @RequestMapping(value = "test", method= RequestMethod.GET) ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์ง€๋งŒ Spring 4.3๋ฒ„์ „๋ถ€ํ„ฐ Method Mapping์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉฐ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์กŒ๋‹ค.

@RequestMapping

@RequestMapping์˜ ๊ฒฝ์šฐ HTTP ์š”์ฒญ๊ณผ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ Mapping ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ HTTP ๋ฉ”์„œ๋“œ, ๊ฒฝ๋กœ, ์š”์ฒญ ํ—ค๋”, ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋“ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์š”์ฒญ์„ Mapping ํ•  ์ˆ˜ ์žˆ๋‹ค.

@RequestMapping์˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์‚ฌ์šฉ๋ฒ•

@RequestMapping์˜ ๊ฒฝ์šฐ Class์™€ Method์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. value๋ฅผ ์‚ฌ์šฉํ•ด url์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , method๋ฅผ ์‚ฌ์šฉํ•ด ์ ์šฉ๋  method๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ๋ณธ์ ์ธ Mapping ๋ฐฉ๋ฒ•

@RequestMapping(value = "/test",method = RequestMethod.GET)
public String testView(){
	return "test";
}

๋ฉ”์†Œ๋“œ์— value์™€ method๋ฅผ ์‚ฌ์šฉํ•œ URL Mapping์ด๋‹ค. GET ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™๋˜๋ฉฐ, "/test" URL๋กœ ์ ‘๊ทผ์‹œ ์ž‘๋™๋œ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ Method๋„ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ๋ฐ, POST, PUT, PATCH, DELETE ๋“ฑ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” 5๊ฐ€์ง€์˜ Method๊ฐ€ ์žˆ๋‹ค.

Class์— URL์„ ์ง€์ •ํ•˜์—ฌ ๋ฉ”์†Œ๋“œ์— ๊ณตํ†ต URL path ์„ค์ •

@Controller
@RequestMapping("/page")
public class Test{
	@RequestMapping(value = "/test", method = RequestMethod.GET)
    public String a(){
    	return "test";
    }
}

ํด๋ž˜์Šค์— ์ ์šฉํ•˜์—ฌ URL path๋ฅผ ์ง€์ •ํ•˜๊ณ , ํ•˜์œ„ ๋ฉ”์†Œ๋“œ์—๋Š” "/test" URL์„ Mappingํ•˜์—ฌ "/page/a" ์—๋Œ€ํ•œ URL๊ฒฝ๋กœ๋ฅผ ์ ์šฉํ•˜์˜€๋‹ค. ์ด๋ ‡๋“ฏ ํด๋ž˜์Šค ๋˜๋Š” ๋ฉ”์†Œ๋“œ์— ์ ์šฉ์œ„์น˜์— ๋”ฐ๋ผ ์ ์šฉ์ด ๋‹ค๋ฅด๊ฒŒ ๋œ๋‹ค.

์—ฌ๋Ÿฌ๊ฐœ์˜ URL์„ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ์— Mapping

@RequestMapping({"test1","test2"})
public String test(){
	return "test";
}

value์— {}์™€ ๊ฐ™์€ ๋ฐฐ์—ด์„ ์–ธ์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ๊ฐœ์˜ URL์„ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ์— Mapping ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๊ฒฝ๋กœ ๋ณ€์ˆ˜ ์‚ฌ์šฉ

@RequestMapping("/test/{id}")
public String test(
	@PathVariable String id
	){
	return "test";
			}

@PathVariable์„ ์‚ฌ์šฉํ•œ URL ๊ฒฝ๋กœ ๋ณ€์ˆ˜ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐ๊ฑด Mapping

@RequestMapping(value = "/test", method = RequestMethod.GET, params = "test=true")
@ResponseBody
public String test() {
    return "test";
}


ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐ๊ฑด๊ฐ’์ด ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ 400์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ

ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ๋Š” ์ •์ƒ ์กฐ๊ฑด์—์„œ๋Š” ์ž˜ ์ž‘๋™๋œ๋‹ค.

ํ—ค๋” ์กฐ๊ฑด Mapping

@RequestMapping(value = "/test", method = RequestMethod.GET, headers = "test=true")
@ResponseBody
public String test() {
    return "test";
}


ํ—ค๋”์˜ ๊ฐ’์ด ์—†์„๊ฒฝ์šฐ 404์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ

test = true ๊ฐ€ ์žˆ์„๊ฒฝ์šฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฏธ๋””์–ดํƒ€์ž…์— ๋”ฐ๋ฅธ ์กฐ๊ฑด Mapping

@GetMapping(value = "/test", consumes = "application/json")
@ResponseBody
public String test() {
    return "test";
}


๋ฏธ๋””์–ด ํƒ€์ž…์ด Mapping ์กฐ๊ฑด์˜ ๋ฏธ๋””์–ดํƒ€์ž…๊ณผ ๋‹ฌ๋ฆฌ 415์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋™์ผํ•  ๊ฒฝ์šฐ ์ •์ƒ ์ž‘๋™ํ•œ๋‹ค.


@MethodMapping

Method Mapping์˜ ๊ฒฝ์šฐ Spring 3.4๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ Mapping ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ๊ธฐ์กด @RequestMapping์˜ method ๋ถ€๋ถ„๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ "@GetMapping" ์ด์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•œ๋‹ค. RequestMapping๊ณผ ๋‹ค๋ฅธ์ ์€ ํด๋ž˜์Šค์— ์ ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ข…๋ฅ˜๋Š” ๋งŽ์ง€๋งŒ ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ GetMapping์„ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค. ํ•˜์ง€๋งŒ RestAPI๋ฅผ ๋งŒ๋“ค๊ธฐ์œ„ํ•ด์„  ์ ์ ˆํ•˜๊ฒŒ ๊ธฐ๋Šฅ์— ๋งž๋Š” Mapping์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

@GetMapping

@GetMapping("/test")
public User test(){
  return "test";
}

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ Mapping์ธ GetMapping์ด๋‹ค. ํ†ต์ƒ์ ์œผ๋กœ URL ์ด๋™์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์†ก์‹œ URL์„ ์ด์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์ด ์ทจ์•ฝํ•˜๋‹ค. ๋ฆฌ์†Œ์Šค์˜ ์กฐํšŒ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

@PostMapping

@PostMapping("/test")
public User test(){
  return "test";
}

PostMapping, Server์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ํ”ํžˆ ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž…, form์„ ์ด์šฉํ•œ ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ๋“ฑ์— ์‚ฌ์šฉ๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ๊ฐ€ Header์˜ ๋ณธ๋ฌธ์„ ํ†ตํ•ด ์ „์†ก๋˜๊ธฐ ๋•Œ๋ฌธ์— Get๋ฐฉ์‹์— ๋น„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ๋ณด์•ˆ์ด ๊ฐ•ํ•˜๋‹ค๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค.

@PutMapping

@PutMapping("/test")
public User test(){
  return "test";
}

PutMapping, Server์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์ˆ˜์ •ํ•˜๊ณ ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์†ก๋ฐฉ์‹์€ Post์™€ ๋™์ผํ•˜๊ฒŒ Header์˜ ๋ณธ๋ฌธ์„ ํ†ตํ•ด ์ „๋‹ฌ๋œ๋‹ค.

@PatchMapping

@PatchMapping("/test")
public User test(){
  return "test";
}

PatchMapping, Server์˜ ๋ฆฌ์†Œ์Šค ์ผ๋ถ€๋ฅผ ์ˆ˜์ •ํ•˜๊ณ ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. Put๊ณผ ๋‹ค๋ฅธ์ ์€ ์ผ๋ถ€ ์ˆ˜์ •ํ•œ๋‹ค๋Š”๊ฒƒ์— ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์†ก๋ฐฉ์‹์€ Post์™€ ๋™์ผํ•˜๊ฒŒ Header์˜ ๋ณธ๋ฌธ์„ ํ†ตํ•ด ์ „๋‹ฌ๋œ๋‹ค.

@DeleteMapping

@DeleteMapping("/test")
public User test(){
  return "test";
}

DeleteMapping, ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ Server์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ• ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์†ก ์—ญ์‹œ Post์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๊ณ  Header์˜ ๋ณธ๋ฌธ์„ ํ†ตํ•ด ์ „์†ก๋œ๋‹ค.


๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” Get, Post์˜ ์š”์ฒญ๋งŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Get, Post ๋ฐฉ์‹์œผ๋กœ ์ ์šฉ๋œ๋‹ค. ๋งŒ์•ฝ ๋‹ค๋ฅธ Put, Patch, Delete ์˜ method๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค.

1. input-hidden ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

 <form action="/test" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <button type="submit">์ „์†ก</button>
</form>

form ํƒœ๊ทธ๋Š” POST๋กœ ์ง€์ •ํ•˜๊ณ  ๋‚ด๋ถ€ input type hidden ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑ ํ›„ name์„ "_method" ๋กœ ์ง€์ •, value="PUT"์œผ๋กœ ํ•˜๋ฉด Server์—์„œ ํ•ด๋‹น ๊ฐ’์„ ์ฝ๊ณ  PUTMapping์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

2. ajax๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•

<script>
   function testPutRequest() {
       var xhr = new XMLHttpRequest();
       xhr.open('PUT', '/test', true);
       xhr.send();
   }
</script>

ajax๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ method ๋ฐฉ์‹์„ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด method์— ๋งž๊ฒŒ ์ฒ˜๋ฆฌ๋œ๋‹ค.

profile
๋„๋ฆฌ๋„๋ฆฌ๋‹ด๋‹ด ๐Ÿค๐Ÿฅ๐Ÿค๐Ÿฅ๐Ÿฃ๐Ÿฃ

0๊ฐœ์˜ ๋Œ“๊ธ€