Contents

RestClientException ์ฒ˜๋ฆฌ

Spring ํ™˜๊ฒฝ์—์„œ (Spring5๋Š” ๋‹ฌ๋ผ์กŒ์ง€๋งŒ…) ์™ธ๋ถ€ API๋กœ์˜ ํ˜ธ์ถœ์„ ํ• ๋•Œ ์ž์ฃผ ์“ฐ์ด๋Š” RestTemplate. ์ด๋•Œ request์— ๋Œ€ํ•ด ์ •์ƒ์ ์ธ ์‘๋‹ต์ด ์•„๋‹Œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ๋ง‰์—ฐํ•˜๊ฒŒ ์ƒ๊ฐ์„ ํ•ด๋ณด๋ฉด Http Status Code๋ฅผ ๋ฐ›์•„์„œ ํŒ๋ณ„์„ ํ•˜๋ฉด ๋˜์ง€๋งŒ Http Status Code๋งŒ ๋ด๋„ ์—„์ฒญ~๋งŽ๋‹ค. ( ์œ„ํ‚คํ”ผ๋””์•„ ์ฐธ๊ณ  : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes ) if~else ๋กœ ๋‹ค ๋‚˜๋ˆŒ์ˆ˜๋„ ์—†๊ณ … ์šฐ์„  ์„ฑ๊ณต/์‹คํŒจ์— ๋Œ€ํ•œ ํŒ๋ณ„์€ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ํŒŒ๊ณ ํŒŒ๊ณ  ๋“ค์–ด๊ฐ€๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๊ฒ ๋‹ค.

Http Status Code ๊ทธ๋ฃน์ •์˜

Spring-project github์— ๊ฐ€๋ณด๋ฉด HttpStatus ํ•˜์œ„์— ๋‚ด๋ถ€ Enum์œผ๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ์ •์˜๋˜์–ด ์žˆ๋‹ค. ( ๋งํฌ )

public enum HttpStatus {
	
	...

	public Series series() {
		return Series.valueOf(this);
	}

	...

	public enum Series {

		INFORMATIONAL(1),
		SUCCESSFUL(2),
		REDIRECTION(3),
		CLIENT_ERROR(4),
		SERVER_ERROR(5);

		...

		public static Series valueOf(int status) {
			int seriesCode = status / 100;
			for (Series series : values()) {
				if (series.value == seriesCode) {
					return series;
				}
			}
			throw new IllegalArgumentException("No matching constant for [" + status + "]");
		}
	}
}	

์•ฝ 80์—ฌ๊ฐœ ์‘๋‹ต์ฝ”๋“œ๋“ค์„ ํฌ๊ฒŒ 5๊ฐœ ๋ฌถ์Œ์œผ๋กœ ์ •์˜ํ•ด ๋†“์€๊ฒƒ์„ ๋ณผ์ˆ˜์žˆ๋‹ค. ์ฆ‰, 201 / 201 / 202 ๊ฐ™์€ ๋…€์„๋“ค์€ ์ „๋ถ€ SUCCESSFUL ์„ฑ๊ณต ์œผ๋กœ ๊ทธ๋ฃนํ•‘์ด ๋˜๋Š”๊ฒƒ์„ ํ™•์ธํ• ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿผ RestTemplate ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋‚˜?

์ฝ”๋“œ๋ฅผ ์ญ‰์ญ‰ ๋”ฐ๋ผ๊ฐ€๋‹ค ๋ณด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ 4xx, 5xx ๋Š” ์—๋Ÿฌ๋ผ๊ณ  ํŒ๋‹จํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ผ RestClientException ์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฒƒ์„ ํ™•์ธํ• ์ˆ˜ ์žˆ๋‹ค.

try {
	ClientHttpRequest request = createRequest(url, method);
	if (requestCallback != null) {
		requestCallback.doWithRequest(request);
	}
	response = request.execute();
	handleResponse(url, method, response);
	if (responseExtractor != null) {
		return responseExtractor.extractData(response);
	}
	else {
		return null;
	}
}
catch (IOException ex) {
protected boolean hasError(HttpStatus statusCode) {
	return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR || statusCode.series() == HttpStatus.Series.SERVER_ERROR);
}

์„ฑ๊ณต/์‹คํŒจ ์ฒ˜๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํ• ๊ฒƒ์ธ๊ฐ€

๊ฐ์ž ์ •์˜ํ•˜๊ธฐ ๋‚˜๋ฆ„์ผ๊ฒƒ ๊ฐ™๋‹ค. ์šฐ์„  ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ RestTemplate ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์—ฌ ์ •์˜๋œ ๋Œ€๋กœ 4xx, 5xx๊ฐ€ ์—๋Ÿฌ๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™๊ณ 

try {
    responseBody = restTemplate.postForObject(url, httpEntity, byte[].class);
} catch (RestClientException e) { // ์—๋Ÿฌ์ธ ๊ฒฝ์šฐ RestClientException ์„ ๋‚ด๋ฑ‰๋Š”๋‹ค.
    log.error("##### restTemplate error, url = {}", url, e);
}

์ •์˜๋œ ์—๋Ÿฌ(?)์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด DefaultResponseErrorHandler์„ ์ƒ์†๋ฐ›๊ณ  hasError๋ฉ”์†Œ๋“œ๋ฅผ ๋ฌด์กฐ๊ฑด ํŒจ์Šคํ•˜๋„๋ก Override ํ•˜๊ณ ๋‚œ ๋‹ค์Œ ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„์ˆ˜ ์žˆ๊ฒ ๋‹ค. (๋ฌผ๋ก  ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ• ๋ง๊ณ  ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊ฒƒ ๊ฐ™๋‹ค.)


Buy me a coffeeBuy me a coffee