๐จ Error :
๊ฐ๋ฐ ๋์ค์ ๋์ค์ฝ๋ ์นํ
์ผ๋ก ์๋ฆผ ๊ธฐ๋ฅ์ ๊ตฌํํ๋๋ฐ ๋์์ ์ ์์ ์ผ๋ก ๋๋ ์์ ๊ฐ์ ์๋ฌ๊ฐ ๊ณ์ ๋ฐ์ํ๋ค.
๐ค ์์ธ :
WebClient์ ๋ํด ๊น๊ฒ ์์ง ๋ชปํ ์ํ์์ ์ฌ์ฉํ์ฌ ๊ฝค๋ ์ ๋ฅผ ๋จน์๋ค. ๊ฒฐ๋ก ๋ถํฐ ์๊ธฐํ๋ฉด ๋์ค์ฝ๋ ์นํ ์ ์๋ต ์ํ ์ฝ๋๋ก 204 No Content๋ฅผ ๋ฐํํ๊ณ , WebClient์์ toEntity() ๋ฉ์๋๋ 200 ๋ ์ํ ์ฝ๋๋ ์๋ฌ๊ฐ ์๋๋ผ๊ณ ํ๋จํ์ง๋ง 204 ์ํ ์ฝ๋์ ๊ฒฝ์ฐ ์๋ฌ๋ผ๊ณ ํ๋จํ๋ค.
์ธ๋ถ ์๋ฒ์์ ํต์ ์์ api ์๋ต ์ํ ์ฝ๋๋ฅผ ์์๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ์ด์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ๋ฐ๋ก ํด์ฃผ๋ฉด ๋๋ค.
return webClient
.post()
.uri(discordSecurityWebHookUrl)
.header("Content-Type", "application/json")
.bodyValue(json)
.retrieve()
.toEntity(String.class);
- ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ธฐ์กด ์ฝ๋ ๋ด์ฉ์ด๋ค.
- toEntity(Class<T> responseType) ๋ฉ์๋๋ WebClient์ ์๋ต์ ResponseEntity<T>๋ก ๋ณํํ๋ ์ญํ ์ ํ๋ค.
- ์ฆ, WebClient๋ฅผ ํตํด HTTP ์์ฒญ์ ๋ณด๋ธ ํ ์๋ฒ๋ก๋ถํฐ์ ์๋ต์ ResponseEntity ๊ฐ์ฒด๋ก ๊ฐ์ธ์ ๋ฐํํด์ค๋ค.
- ๊ทธ๋ฐ๋ฐ toEntity ๋ฉ์๋๋ ์๋ต ๋ฐ๋๊ฐ ๋ฐ๋์ ํ์ํ๋ค๊ณ ๊ฐ์ ํ๋ฏ๋ก 204 ์ํ ์ฝ๋๊ฐ ๋ฐํ๋๋ฉด ๋ฐ๋๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
- ์ด ๊ฒฝ์ฐ WebClient๋ ์ด๋ฅผ ์๋ฌ๋ก ๊ฐ์ฃผํ์ฌ WebClientResponseException์ ๋์ง๋ค.
๐ ํด๊ฒฐ :
return webClient
.post()
.uri(discordSecurityWebHookUrl)
.header("Content-Type", "application/json")
.bodyValue(json)
.retrieve()
.bodyToMono(String.class)
.map(ResponseEntity::ok)
.onErrorResume(WebClientResponseException.class, ex -> {
// 204 No Content ์ํ ์ฝ๋ ์ฒ๋ฆฌ
if (ex.getStatusCode() == HttpStatus.NO_CONTENT) {
return Mono.empty(); // 204 ์ํ์ฝ๋์ธ ๊ฒฝ์ฐ ๋น Mono ๋ฐํ
}
// ๊ธฐํ ์๋ฌ ์ฒ๋ฆฌ
log.error("Error :", ex);
return Mono.error(ex);
});
.bodyToMono(String.class)
- toEntity๋ ์ํ์ฝ๋ 204๋ฅผ ์ฒ๋ฆฌํ ์ ์์ผ๋ bodyToMono๋ก ์๋ต ๋ฐ๋๋ฅผ String์ผ๋ก ๋ณํํ์ฌ Mono<String>์ ๋ฐํํ๊ฒ ํ๋ค.
.map(ResponseEntity::ok)
- Mono<String> ๊ฐ์ ์ํ ์ฝ๋ 200 OK๋ก ์ค์ ํ ResponseEntity<String>์ผ๋ก ๋ณํํ๋ค.
- toEntity๋ฅผ ๋ ๋จ๊ณ๋ก ๋๋ ๊ฒ์ด๋ค.
.onErrorResume(WebClientResponseException.class, ex -> {...});
- ์์ ์ํ๋ก๋ผ๋ฉด ๋ค์ 204 No Content๋ก ์ธํด ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. ๊ทธ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ก์ง์ด๋ค.
if (ex.getStatusCode() == HttpStatus.NO_CONTENT)
- 204 No Content์ผ ๊ฒฝ์ฐ ๋น ๋ชจ๋ ธ๋ฅผ ๋ฐํํ๊ฒ ํ๊ณ
return Mono.error(ex);
- ๋ค๋ฅธ 400-500 ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ํด๋น ์๋ฌ๋ฅผ ์ถ๋ ฅํ๊ณ ์๋ฌ ์ ๋ณด๋ฅผ ๋ฆฌํดํ๋ค.
์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋ฉด 204 No Content๋ก ์๋ต์ด ์๋ ๋น Mono๋ฅผ ๋ฐํํ์ฌ ์๋ฌ๊ฐ ๋ฐ์๋์ง ์๊ฒ ๋ง์ ์ ์๋ค.
๐ค ์๋ฌธ์ :
์์
์ฐธ๊ณ
Chat GPT