package com.ydtech.config.resttemplate; import com.ydtech.exception.SystemException; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.web.client.ResponseErrorHandler; import java.io.IOException; public class CustomResponseErrorHandler implements ResponseErrorHandler { @Override public boolean hasError(ClientHttpResponse response) throws IOException { int rawStatusCode = response.getRawStatusCode(); return rawStatusCode > 200; } // 处理错误 @Override public void handleError(ClientHttpResponse response) throws IOException { if (response.getStatusCode() == HttpStatus.REQUEST_TIMEOUT) { throw new SystemException("网络超时, 请重试!"); } if (response.getStatusCode().value() > 500) { throw new SystemException("网络异常, 请重试!"); } throw new SystemException("请求异常" + response.getStatusText()); } }