您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Spring Cloud Gateway自定義異常處理Exception Handler的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
自定義一個 GlobalErrorAttributes:
@Component public class GlobalErrorAttributes extends DefaultErrorAttributes{ @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { Throwable error = super.getError(request); Map<String, Object> map = super.getErrorAttributes(request, options); map.put("status", HttpStatus.BAD_REQUEST.value()); map.put("message", error.getMessage()); return map; } }
實現一個
@Component @Order(-2) public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { public GlobalErrorWebExceptionHandler(GlobalErrorAttributes gea, ApplicationContext applicationContext, ServerCodecConfigurer serverCodecConfigurer) { super(gea, new WebProperties.Resources(), applicationContext); super.setMessageWriters(serverCodecConfigurer.getWriters()); super.setMessageReaders(serverCodecConfigurer.getReaders()); } //渲染html或json @Override protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } private Mono<ServerResponse> renderErrorResponse(final ServerRequest request) { final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); return ServerResponse.status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(errorPropertiesMap)); } }
//Spring 默認的就很好了. @Component public class GatewayErrorAttributes extends DefaultErrorAttributes { private static final Logger logger = LoggerFactory.getLogger(GatewayErrorAttributes.class); @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { Throwable error = super.getError(request); Map<String, Object> errorAttributes = new HashMap<>(8); errorAttributes.put("message", error.getMessage()); errorAttributes.put("method", request.methodName()); errorAttributes.put("path", request.path()); MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations .from(error.getClass(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(ResponseStatus.class); HttpStatus errorStatus = determineHttpStatus(error, responseStatusAnnotation); //必須設置, 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性, 重新實現 DefaultErrorWebExceptionHandler也可. errorAttributes.put("status", errorStatus.value()); errorAttributes.put("code", errorStatus.value()); //html view用 errorAttributes.put("timestamp", new Date()); //html view 用 errorAttributes.put("requestId", request.exchange().getRequest().getId()); errorAttributes.put("error", errorStatus.getReasonPhrase()); errorAttributes.put("exception", error.getClass().getName()); return errorAttributes; } //從DefaultErrorWebExceptionHandler中復制過來的 private HttpStatus determineHttpStatus(Throwable error, MergedAnnotation<ResponseStatus> responseStatusAnnotation) { if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getStatus(); } return responseStatusAnnotation.getValue("code", HttpStatus.class).orElse(HttpStatus.INTERNAL_SERVER_ERROR); } }
這樣就可以了.
注意注意: 必須設置 errorAttributes.put("status", errorStatus.value()) , 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性. 除非你自己像方法一一樣重新實現 DefaultErrorWebExceptionHandler.
然后在網關中訪問一個不存在的服務, 即可看到效果.
curl 'http://127.0.0.1:8900/fundmain22/abc/gogogo?id=1000' --header 'Accept: application/json'
{"exception":"org.springframework.web.server.ResponseStatusException","path":"/fundmain22/abc/gogogo","code":404,"method":"GET","requestId":"094e53e5-1","message":"404 NOT_FOUND","error":"Not Found","status":404,"timestamp":"2021-08-09T11:07:44.106+0000"}
感謝各位的閱讀!關于“Spring Cloud Gateway自定義異常處理Exception Handler的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。