๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒพBackEnd/๐ŸŒฑ Spring

[ RFC ] ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ†ตํ•˜์—ฌ Delete์˜ Status๋ฅผ ์•Œ์•„๋ณด์ž

by MuGeon Kim 2023. 3. 12.
๋ฐ˜์‘ํ˜•

๊ธ€์„ ์ž‘์„ฑํ•œ ์ด์œ 

status์ฝ”๋“œ๋ฅผ ํ•™์Šตํ•˜๊ณ  ์„ฑ๊ณตํ•˜๋ฉด ๋ฌด์กฐ๊ฑด OK(200) ๋ผ๋Š” ๊ณต์‹์ด ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์œผ๋ฉด์„œ ์™„์ „ํžˆ ์ž˜๋ชป ์ดํ•ดํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด ์ดํ›„์— ๋ณต๊ธฐ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ๋‹ค.

https://www.rfc-editor.org/rfc/rfc2616#section-9.7

 

RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1

 

www.rfc-editor.org

 

๊ธฐ์กด์— ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๋˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž

    @DeleteMapping("{id}")
    @ResponseStatus(HttpStatus.OK)
    public void delete(@PathVariable Long id){
        this.userService.delete(id);
    }

๋‚ด๊ฐ€ ์ด ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„์„ ํ•˜๋ฉด์„œ ์ƒ๊ฐํ•œ ๋ฐฉ์‹์€ ์‚ญ์ œ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋ฉด ์„ฑ๊ณต์„ ์˜๋ฏธํ•˜๋Š” OK(200)๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ControllerAdvice๋ฅผ ํ†ตํ•˜์—ฌ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

  mockMvc.perform(delete("/user/1")
                .contentType(APPLICATION_JSON))
                .andExpect(status().isOk())

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ์„ ํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜์—ฌ given()์„ ํ†ตํ•ด ์ƒ์„ฑ๋œ ์œ ์ €๋ฅผ delete๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด ๋ถ€๋ถ„์—์„œ ์ž˜๋ชป๋œ ๋ถ€๋ถ„์€ ์œ„์— ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด ์‘๋‹ตํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๋•Œ๋Š” 204๋กœ ์‘๋‹ตํ•˜๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค.

A successful response SHOULD be 200 (OK) if the response includes an
entity describing the status, 202 (Accepted) if the action has not
yet been enacted, or 204 (No Content) if the action has been enacted
but the response does not include an entity.

 

 

 

๋ฐ˜์‘ํ˜•