spring boot 2 統一異常處理
阿新 • • 發佈:2018-09-05
auto cati uil extend 代碼 自定義error view empty framework
spring mvc 針對controller層異常統一處理非常簡單,使用 @RestControllerAdvice 或 @RestControllerAdvice 註解就可以輕@RestControllerAdvice
public class GatewayExceptionHandler { /*@ExceptionHandler(Exception.class) public JsonResult handleBusinessException(HttpServletRequest request, Exception e) { e.printStackTrace(); String code = ErrorCodeEnum.SYSTEM_ERROR_STRING.getCode(); String message = StringUtils.isNotEmpty(e.getMessage()) ? e.getMessage() : "Service Currently Unavailable"; return JsonResult.ErrorResponse(code, message); }*/ @ExceptionHandler(value = Exception.class) public Map errorHandler(Exception ex) { Map map = new HashMap(); map.put("code", 100); map.put("msg", ex.getMessage()); return map; } }
下面記錄一下,spring cloud gateway項目中重寫 DefaultErrorWebExceptionHandler 類,實現自定義異常處理
首先寫一個類繼承 DefaultErrorWebExceptionHandler 類,重寫方法
public class RmcloudExceptionHandler extends DefaultErrorWebExceptionHandler { /** * Create a new {@code DefaultErrorWebExceptionHandler} instance. * * @param errorAttributes the error attributes * @param resourceProperties the resources configuration properties *@param errorProperties the error configuration properties * @param applicationContext the current application context */ public RmcloudExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) { super(errorAttributes, resourceProperties, errorProperties, applicationContext); } /** * 確定返回什麽HttpStatus * * @param errorAttributes * @return */ @Override protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) { //HttpStatus status = (HttpStatus) errorAttributes.get("status"); // return HttpStatus.INTERNAL_SERVER_ERROR == status ? HttpStatus.OK : status; return HttpStatus.OK; } /** * 返回的錯誤信息json內容 * * @param request * @param includeStackTrace * @return */ @Override protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) { Throwable error = this.getError(request); return JsonResult.responseReturnMap(RmcloudConstant.GATEWAY_ERRORCODE, this.buildMessage(request, error)); } private String buildMessage(Throwable t) { return "未知錯誤!"; } private String buildMessage(ServerRequest request, Throwable ex) { StringBuilder message = new StringBuilder("api-gateway Failed to handle request ["); message.append(request.methodName()); message.append(" "); message.append(request.uri()); message.append("]"); if (ex != null) { message.append(": "); message.append(ex.getMessage()); } return message.toString(); } private HttpStatus determineHttpStatus(Throwable error) { return error instanceof ResponseStatusException ? ((ResponseStatusException) error).getStatus() : HttpStatus.INTERNAL_SERVER_ERROR; } }
然後,配置自定義的ExceptionHandler
import com.vcredit.rmcloud.gateway.exception.RmcloudExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import java.util.Collections;
import java.util.List;
/**
* webflux全局異常處理器配置配置
* 由於webflux的函數式編程方式中不能通過controllerAdvice只能通過每個RouterFunction中添加filter的方式實現異常處理,
* 這裏通過註入一個自定義ErrorWebExceptionHandler來達到全局異常處理的目的
*
* @author lee
*/
@Configuration
@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
public class ErrorHandlerConfiguration {
private final ServerProperties serverProperties;
private final ApplicationContext applicationContext;
private final ResourceProperties resourceProperties;
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public ErrorHandlerConfiguration(ServerProperties serverProperties,
ResourceProperties resourceProperties,
ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer,
ApplicationContext applicationContext) {
this.serverProperties = serverProperties;
this.applicationContext = applicationContext;
this.resourceProperties = resourceProperties;
this.viewResolvers = viewResolversProvider
.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ErrorWebExceptionHandler errorWebExceptionHandler(
ErrorAttributes errorAttributes) {
RmcloudExceptionHandler exceptionHandler = new RmcloudExceptionHandler(
errorAttributes, this.resourceProperties,
this.serverProperties.getError(), this.applicationContext);
exceptionHandler.setViewResolvers(this.viewResolvers);
exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
return exceptionHandler;
}
}
JsonResult內容
@Data @NoArgsConstructor @AllArgsConstructor public class JsonResult<T> { private static String successCode = ""; private String errorCode; private String msg; private T data; private Long timestamp; public static <T> JsonResult<T> successResponse(T data) { return new JsonResult<>(successCode, "Success", data, System.currentTimeMillis()); } public static <T> JsonResult<T> errorResponse(String errorMessage) { return new JsonResult<>(RmcloudConstant.GATEWAY_ERRORCODE, errorMessage, null, System.currentTimeMillis()); } public static <T> JsonResult<T> errorResponse(String status, String errorMessage) { return new JsonResult<>(status, errorMessage, null, System.currentTimeMillis()); } public static Map<String, Object> responseReturnMap(String status, String errorMessage) { Map<String, Object> map = new HashMap<>(); map.put("errorCode", status); map.put("msg", errorMessage); map.put("data", null); return map; } }
最後感謝chenqian56131,主要代碼是從他github上淘來的,以上是結合實際項目的應用,記錄下來,方便以後查閱。
spring boot 2 統一異常處理