當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring cloud gateway的自定义异常响应
生活随笔
收集整理的這篇文章主要介紹了
Spring cloud gateway的自定义异常响应
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于gateway默認的返回碼和返回信息不能滿足下游微服務的自定義的異常響應碼和描述,則需要通過網關透傳下游微服務的異常的碼值或者至少異常描述信息。
spring cloud gateway給我們留了口子,只需要繼承類DefaultErrorWebExceptionHandler,并覆蓋相應的接口,并且聲明ErrorWebExceptionHandler配置即可,下面貼出代碼:
1、 ErrorWebExceptionHandler配置
@Configuration @EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class}) public class ExceptionHandlerConfiguration {private final ServerProperties serverProperties;private final ApplicationContext applicationContext;private final ResourceProperties resourceProperties;private final List<ViewResolver> viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public ExceptionHandlerConfiguration(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) {JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(errorAttributes,this.resourceProperties,this.serverProperties.getError(),this.applicationContext);exceptionHandler.setViewResolvers(this.viewResolvers);exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());return exceptionHandler;}}2、自定義DefaultErrorWebExceptionHandler異常處理類
public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {private static Logger logger = LoggerFactory.getLogger(JsonExceptionHandler.class);public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,ErrorProperties errorProperties, ApplicationContext applicationContext) {super(errorAttributes, resourceProperties, errorProperties, applicationContext);}/*** 獲取異常屬性*/@Overrideprotected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {int code = HttpStatus.INTERNAL_SERVER_ERROR.value();Throwable error = super.getError(request);if (error instanceof org.springframework.cloud.gateway.support.NotFoundException) {code = HttpStatus.NOT_FOUND.value();} // return response(code, this.buildMessage(request, error));return response(code, error.getMessage());}/*** 指定響應處理方法為JSON處理的方法* @param errorAttributes*/@Overrideprotected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);}/*** 根據code獲取對應的HttpStatus* @param errorAttributes*/@Overrideprotected int getHttpStatus(Map<String, Object> errorAttributes) {int statusCode = (int) errorAttributes.get("code"); // return HttpStatus.valueOf(statusCode);return statusCode;}/*** 構建異常信息* @param request* @param ex* @return*/private String buildMessage(ServerRequest request, Throwable ex) {StringBuilder message = new StringBuilder("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();}/*** 構建返回的JSON數據格式* @param status 狀態碼* @param errorMessage 異常信息* @return*/public static Map<String, Object> response(int status, String errorMessage) {Map<String, Object> map = new HashMap<>();map.put("code", status);map.put("message", errorMessage);map.put("data", null);logger.error(map.toString());return map;} } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Spring cloud gateway的自定义异常响应的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud Gateway
- 下一篇: Springboot集成nacos实现服