springframework.http.ResponseEntity

๐Ÿ”” ResponseEntity ๊ฐœ์š” ๐Ÿ”” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๊ณผ ๋น„๊ต What? Why? ์Šคํ”„๋ง 3 ๋ฒ„์ „๋ถ€ํ„ฐ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ, ์Šคํ”„๋ง MVC์—์„œ HTTP ์‘๋‹ต์„ ๋‹ค๋ฃจ๋Š” ์ฃผ์š” ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์ด๋‹ค. HTTP ์‘๋‹ต์˜ ์ „์ฒด ๋‚ด์šฉ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์š”์†Œ๋“ค์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒํƒœ ์ฝ”๋“œ (HTTP Status Code) : 200 OK, 404 Not Found, 500 Internal Server Error ๋“ฑ ํ—ค๋” (Headers) : ์‘๋‹ต ํ—ค๋”์— ํŠน์ • ๊ฐ’์„ ์ถ”๊ฐ€/์ˆ˜์ • ๋ณธ๋ฌธ (Body) : ์‹ค์ œ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ๋  ๋ฐ์ดํ„ฐ @GetMapping("/example") public ResponseEntity<String> getExample() { String body = "Hello, World!"; HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "CustomValue"); return new ResponseEntity<>(body, headers, HttpStatus.OK); } @ResponseBody ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™”ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ, ์ด๊ฒƒ๋งŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ํ—ค๋”๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค. @ResopnseStatus๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ—ค๋”๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•ด์•ผํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ...

January 24, 2025 11:05 ยท 1 min ยท 195 words ยท nakji