您好,登錄后才能下訂單哦!
SpringCloud Finchley Gateway 統一異常處理
全文搜索[@@]搜索重點內容標記
1 . 問題:使用SpringCloud Gateway時,會出現各種系統級異常,默認返回HTML.
2 . Finchley版本的Gateway,使用WebFlux形式作為底層框架,而不是Servlet容器,所以常規的異常處理無法使用
翻閱源碼,默認是使用DefaultErrorWebExceptionHandler
這個類實現結構如下:
可以實現參考DefaultErrorWebExceptionHandler
和AbstractErrorWebExceptionHandler
自定義實現ErrorWebExceptionHandler
,然后,注冊為Bean到Spring容器中即可(Bean Name:"errorWebExceptionHandler"
)
具體實現代碼如下:
package pro.chenggang.example.spring.cloud.gateway.support; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.cloud.gateway.support.NotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @classDesc: 統一異常處理,參考{@link org.springframework.web.server.AbstractErrorWebExceptionHandler}修改 * @author: chenggang * @createTime: 2018/10/30 */ public class JsonExceptionHandler implements ErrorWebExceptionHandler { private static final Logger log = LoggerFactory.getLogger(JsonExceptionHandler.class); /** * MessageReader */ private List<HttpMessageReader<?>> messageReaders = Collections.emptyList(); /** * MessageWriter */ private List<HttpMessageWriter<?>> messageWriters = Collections.emptyList(); /** * ViewResolvers */ private List<ViewResolver> viewResolvers = Collections.emptyList(); /** * 存儲處理異常后的信息 */ private ThreadLocal<Map<String,Object>> exceptionHandlerResult = new ThreadLocal<>(); /** * 參考AbstractErrorWebExceptionHandler * @param messageReaders */ public void setMessageReaders(List<HttpMessageReader<?>> messageReaders) { Assert.notNull(messageReaders, "'messageReaders' must not be null"); this.messageReaders = messageReaders; } /** * 參考AbstractErrorWebExceptionHandler * @param viewResolvers */ public void setViewResolvers(List<ViewResolver> viewResolvers) { this.viewResolvers = viewResolvers; } /** * 參考AbstractErrorWebExceptionHandler * @param messageWriters */ public void setMessageWriters(List<HttpMessageWriter<?>> messageWriters) { Assert.notNull(messageWriters, "'messageWriters' must not be null"); this.messageWriters = messageWriters; } @Override public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { /** * 按照異常類型進行處理 */ HttpStatus httpStatus; String body; if (ex instanceof NotFoundException) { httpStatus = HttpStatus.NOT_FOUND; body = "Service Not Found"; }else if(ex instanceof ResponseStatusException) { ResponseStatusException responseStatusException = (ResponseStatusException) ex; httpStatus = responseStatusException.getStatus(); body = responseStatusException.getMessage(); }else{ httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; body ="Internal Server Error"; } /** * 封裝響應體,此body可修改為自己的jsonBody */ Map<String,Object> result = new HashMap<>(2,1); result.put("httpStatus",httpStatus); result.put("body",body); /** * 錯誤記錄 */ ServerHttpRequest request = exchange.getRequest(); log.error("[全局異常處理]異常請求路徑:{},記錄異常信息:{}",request.getPath(),ex.getMessage()); /** * 參考AbstractErrorWebExceptionHandler */ if (exchange.getResponse().isCommitted()) { return Mono.error(ex); } exceptionHandlerResult.set(result); ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders); return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest) .switchIfEmpty(Mono.error(ex)) .flatMap((handler) -> handler.handle(newRequest)) .flatMap((response) -> write(exchange, response)); } /** * 參考DefaultErrorWebExceptionHandler * @param request * @return */ protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) { Map<String,Object> result = exceptionHandlerResult.get(); return ServerResponse.status((HttpStatus) result.get("httpStatus")) .contentType(MediaType.APPLICATION_JSON_UTF8) .body(BodyInserters.fromObject(result.get("body"))); } /** * 參考AbstractErrorWebExceptionHandler * @param exchange * @param response * @return */ private Mono<? extends Void> write(ServerWebExchange exchange, ServerResponse response) { exchange.getResponse().getHeaders() .setContentType(response.headers().getContentType()); return response.writeTo(exchange, new ResponseContext()); } /** * 參考AbstractErrorWebExceptionHandler */ private class ResponseContext implements ServerResponse.Context { @Override public List<HttpMessageWriter<?>> messageWriters() { return JsonExceptionHandler.this.messageWriters; } @Override public List<ViewResolver> viewResolvers() { return JsonExceptionHandler.this.viewResolvers; } } }
注冊Bean
/** * 自定義異常處理[@@]注冊Bean時依賴的Bean,會從容器中直接獲取,所以直接注入即可 * @param viewResolversProvider * @param serverCodecConfigurer * @return */ @Primary @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) { JsonExceptionHandler jsonExceptionHandler = new JsonExceptionHandler(); jsonExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList)); jsonExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters()); jsonExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders()); log.debug("Init Json Exception Handler Instead Default ErrorWebExceptionHandler Success"); return jsonExceptionHandler; }
[@@]注意事項:
1 .上面為示例代碼,其中牽扯到策略工廠和響應封裝的類,可以自定義實現
2 .注冊Bean時依賴的Bean,都會從Spring容器中獲取到
3 .參考此方法思路,可實現統一異常處理,統一封裝錯誤信息。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。