[TIL] 230704 Getter์˜ ์†Œ์ค‘ํ•จ..

CountryGirlยท2023๋…„ 7์›” 4์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
20/80

๐Ÿ“Œ ๋ฌธ์ œ์  & ์‹œ๋„ & ํ•ด๊ฒฐ

๊ฒŒ์‹œ๊ธ€์„ CRUD ํ•˜๋Š” ๊ณผ์ œ๋ฅผ ํ•˜๋ฉด์„œ......

Getter์˜ ์†Œ์ค‘ํ•จ์„ ๋งŽ์ด ๋Š๊ผˆ๋‹ค.

Getter ๊ฐ€ ํ•„์š”ํ•˜๋‚˜? ํ•„์š”ํ• ๊นŒ? ๋ผ๋Š” ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ๋‹ค.

์–ด๋Š ๋‚ ์ด์—ˆ๋‹ค...
api๋ฅผ ์„ค๊ณ„ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค..
ํ•˜์ง€๋งŒ Postman ์—์„œ ๋ณ„๋‚œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค..
401.. 405.. 406.. 415.. 

๋งŽ์ด ๋‚˜์™”๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด 405๋ฅผ ๊ณ ์น˜๋ฉด 406์ด ๋ฐœ์ƒํ•˜๊ณ ..
406์„ ๊ณ ์น˜๋ฉด 416์ด ๋ฐœ์ƒํ•˜๊ณ ...
1-2์‹œ๊ฐ„ ๋™์•ˆ ๊ณ„์† Postman์— ์˜ค๋ฅ˜์™€ ์”จ๋ฆ„์„ ํ–ˆ๋‹ค

.
.
(a few hours later)
.
.

์œ ๋ ˆ์นด.....

Getter๋ฅผ ์•ˆ์จ์„œ ๋‚ฌ๋˜... ์˜ค๋ฅ˜์˜€๋‹ค... 
์ฒ˜์Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋˜ ๋•Œ๋กœ rollback๋ฅผ ํ•˜๊ณ .. 
Getter๋ฅผ ์จ์ฃผ์—ˆ๋‹ค.. 
200์ด ๋‚˜์™”๋‹ค...

<Getter ์˜ ์†Œ์ค‘ํ•จ..>

์ด๋ ‡๊ฒŒ ๊ณ ์ƒ์„ ํ–ˆ์Œ์—๋„ Getter๋ฅผ ์“ฐ์ง€ ์•Š์•„์„œ ๋˜ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค..

๋‚˜๋Š” ๋‹จ์ˆœํžˆ DeleteResponseDto๋ฅผ return ํ•˜๋Š”๋ฐ Getter๊ฐ€ ํ•„์š”ํ•  ์ค„์€ ๋ชฐ๋ž๋‹ค. ๊ทธ๋ƒฅ ํ•„๋“œ๊ฐ’์— final๋กœ ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด์žˆ์–ด์„œ get method๊ฐ€ ํ•„์š”ํ•œ์ง€ ๋ชฐ๋ž๋‹ค.

public class DeleteResponseDto {
    private final String message = "Success Delete";
    private final int statusCode = 200;
}

๊ฒŒ์‹œ๊ธ€์ด Delete๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด

{
    "message": "Success Delete",
    "statusCode": 200
}

Body ์— ์ด๋ ‡๊ฒŒ ๋œจ๊ธธ.. ๊ธฐ๋Œ€ํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ

{
    "timestamp": "2023-07-04T07:57:26.317+00:00",
    "status": 406,
    "error": "Not Acceptable",
    "path": "/api/post/6"
}

Postman Body์—๋Š” ์ด๋ ‡๊ฒŒ ๋‚˜์™”๋‹ค...
๊ทธ๋ ‡์ง€๋งŒ DB๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด delete๋Š” ์‹คํ–‰๋˜์—ˆ๋‹ค.
์‹คํ–‰์€ ๋˜์—ˆ์ง€๋งŒ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์›ํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์˜ค์ง€ ์•Š์•˜์—ˆ๋‹ค..

Getter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@Getter
public class DeleteResponseDto {
    private final String message = "Success Delete";
    private final int statusCode = 200;
}

์ž˜ ๋‚˜์™”๋‹ค.

์ด์   Getter๋Š” ํ•„์ˆ˜์ ์œผ๋กœ ๋ถ™์ผ ๊ฒƒ์ด๋‹ค.
๋งŽ์ด ๋‹นํ–ˆ๋‹ค.

profile
๐Ÿ’ป๐ŸŒพ์‹œ๊ณจ์†Œ๋…€์˜ ์—‰๋ง์ง•์ฐฝ ๊ฐœ๋ฐœ ์„ฑ์žฅ์ผ์ง€๐ŸŒพ๐Ÿ’ป (2023.05.23 ~)

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