色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Cloud Gateway自定義異常處理Exception Handler的方法小結(jié)

瀏覽:43日期:2023-06-26 14:06:42

版本: Spring Cloud 2020.0.3

常見的方法有 實現(xiàn)自己的 DefaultErrorWebExceptionHandler 或 僅實現(xiàn)ErrorAttributes.

方法1: ErrorWebExceptionHandler (僅供示意)

自定義一個 GlobalErrorAttributes:

@Componentpublic 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; }}

實現(xiàn)一個

@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)); }}方法2, 僅實現(xiàn)一個 ErrorAttributes, 以覆蓋默認的 DefaultErrorAttributes

//Spring 默認的就很好了.@Componentpublic 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);//必須設(shè)置, 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性, 重新實現(xiàn) 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中復(fù)制過來的 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); } }

這樣就可以了.

注意注意: 必須設(shè)置 errorAttributes.put('status', errorStatus.value()) , 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性. 除非你自己像方法一一樣重新實現(xiàn) DefaultErrorWebExceptionHandler.

然后在網(wǎng)關(guān)中訪問一個不存在的服務(wù), 即可看到效果.

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'}

感謝網(wǎng)絡(luò)上的各種文章...

到此這篇關(guān)于Spring Cloud Gateway自定義異常處理Exception Handler的文章就介紹到這了,更多相關(guān)Spring Cloud Gateway自定義異常處理內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国内精品久久精品 | 欧美性精品videofree | 久久久夜间小视频 | 亚洲午夜久久久久国产 | 欧美日韩一 | 成人午夜在线 | av18在线播放| 欧美成人精品一级高清片 | 仑乱高清在线一级播放 | 亚洲国产精品成人精品软件 | 日韩欧美色综合 | 免费一级欧美大片视频在线 | 女人张开腿让男人桶视频免费大全 | 久久观看 | 国产亚洲精品自在久久77 | 玖玖99视频 | 波多野结衣福利视频 | 国产91久久精品 | 久久久久久青草大香综合精品 | 乱子伦农村xxxx | 亚洲成 人a影院青久在线观看 | 在线视频亚洲欧美 | 国产精品大全国产精品 | 久草视频网 | 亚洲日本视频在线观看 | 日韩美女一级毛片 | 日韩中文字幕在线观看视频 | 国产精品欧美亚洲韩国日本不卡 | 欧美成人精品高清在线播放 | 日本高清免费视频www | 天天视频一区二区三区 | 26uuu影院亚洲欧美综合 | 99久久精品免费看国产一区二区 | 国产美女又黄又爽又色视频免费 | 国产精品亚洲午夜不卡 | 成人亚洲国产 | 亚洲色吧| 久久99九九精品免费 | 国产三级网站在线观看 | 久久综合久美利坚合众国 | 久久精品国产欧美日韩99热 |