๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL

02/13(ํ™”) ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ Java 35์ผ์ฐจ TIL - ResponseEntity

by ๊ฐ์ž๊ฐœ๋ฐœ๊พผ 2024. 2. 14.

 

๐Ÿ“–  ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

  • ResponseEntity

 

๐Ÿฅ”  ResponseEntity ๋ฅผ ์“ฐ๋Š” ์ด์œ 

Controller ์—์„œ ๊ฐ์ฒด๋ฅผ return ํ•˜๋Š” ๊ฒฝ์šฐ HTTP ์‘๋‹ต์„ ์ œ์–ดํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿด ๋•Œ ResponseEntity ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ ์ ˆํ•œ ์ƒํƒœ ์ฝ”๋“œ์™€ ์‘๋‹ต ํ—ค๋”, ๋ณธ๋ฌธ์„ ์ƒ์„ฑํ•˜์—ฌ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

@GetMapping
    public ResponseEntity<ResponseDto> selectAllUser() {
    User user = userService.selectAllUser();
    
        return ResponseEntity.ok().body(user);

 

๐Ÿฅ”  ResponseEntity ๊ตฌ์กฐ

ResponseEntity ๋Š” HttpEntity ๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์‚ฌ์šฉ์ž์˜ ์‘๋‹ต ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์—

HttpStatus, HttpHeaders, HttpBody ๋ฅผ ํฌํ•จํ•œ๋‹ค.

 

ํ—ค๋”์—๋Š” (์š”์ฒญ/์‘๋‹ต)์— ๋Œ€ํ•œ ์š”๊ตฌ์‚ฌํ•ญ, ๋ฐ”๋””์—๋Š” ๊ทธ ๋‚ด์šฉ์ด ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, ํ—ค๋”์—” ์›น์„œ๋ฒ„๊ฐ€ ์›น๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•˜๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋“ค์–ด์žˆ๊ณ  ๋ฐ”๋””์—๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“š  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

๊ฒ€์ƒ‰ํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด๊ณ  ์œ ์—ฐํ•˜๊ฒŒ ๋ฐ›์•„๋“ค์ด๋Š” ์Šต๊ด€์„ ๋“ค์ด์ž!