刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤
網(wǎng)上關(guān)于刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤,Spring WebFlux響應(yīng)式編程之異常處理的刷卡知識(shí)比較多,也有關(guān)于刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤的問(wèn)題,今天第一pos網(wǎng)(www.fzog.com.cn)為大家整理刷卡常見(jiàn)知識(shí),未來(lái)的我們終成一代卡神。
本文目錄一覽:
1、刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤
刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤
概述Spring WebFlux提供了路由函數(shù)級(jí)別和全局級(jí)別的異常捕獲和處理策略,我們通過(guò)例子演示下使用方法,并比較下各個(gè)策略的優(yōu)勢(shì)。
創(chuàng)建端點(diǎn)示例端點(diǎn)將用戶名作為查詢參數(shù),并返回“Hello name”作為結(jié)果。先創(chuàng)建一個(gè)路由器函數(shù),將/hello請(qǐng)求路由到handleRequest處理回調(diào)。
@Beanpublic RouterFunction<ServerResponse> routeRequest(Handler handler) { return RouterFunctions.route(RequestPredicates.GET("/hello") .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest);}
端點(diǎn)正常調(diào)用“/hello?name=Tonni”,但是如果name參數(shù)缺失,例如“/hello”,則會(huì)引發(fā)異常。接下來(lái)我們將看下如何在WebFlux中處理此異常。
public Mono<ServerResponse> handleRequest(ServerRequest request) { return //... sayHello(request) //...}private Mono<String> sayHello(ServerRequest request) { try { return Mono.just("Hello, " + request.queryParam("name").get()); } catch (Exception e) { return Mono.error(e); }}函數(shù)級(jí)別異常處理
Mono和Flux API中內(nèi)置了兩種方式,用于在函數(shù)級(jí)別處理錯(cuò)誤。
onErrorReturn發(fā)生錯(cuò)誤時(shí),我們可以使用onErrorReturn()返回靜態(tài)默認(rèn)值:
public Mono<ServerResponse> handleRequest(ServerRequest request) { return sayHello(request) .onErrorReturn("Hello Stranger") .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .bodyValue(s));}onErrorResume
一般有三種使用onErrorResume的方式進(jìn)行錯(cuò)誤處理。
計(jì)算動(dòng)態(tài)回退值:
public Mono<ServerResponse> handleRequest(ServerRequest request) { return sayHello(request) .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .bodyValue(s)) .onErrorResume(e -> Mono.just("Error " + e.getMessage()) .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .bodyValue(s)));}
執(zhí)行自定義回退方法:
public Mono<ServerResponse> handleRequest(ServerRequest request) { return sayHello(request) .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .bodyValue(s)) .onErrorResume(e -> sayHelloFallback() .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .bodyValue(s)));}
捕獲、包裝并重新拋出錯(cuò)誤:
public Mono<ServerResponse> handleRequest(ServerRequest request) { return ServerResponse.ok() .body(sayHello(request) .onErrorResume(e -> Mono.error(new NameRequiredException( HttpStatus.BAD_REQUEST, "username is required", e))), String.class);}全局級(jí)別異常處理自定義全局錯(cuò)誤響應(yīng)屬性,可以擴(kuò)展DefaultErrorAttributes類并重寫(xiě)其getErrorAttributes方法:
public class GlobalErrorAttributes extends DefaultErrorAttributes{ @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { Map<String, Object> map = super.getErrorAttributes( request, options); map.put("status", HttpStatus.BAD_REQUEST); map.put("message", "username is required"); return map; }}實(shí)現(xiàn)全局錯(cuò)誤處理,Spring提供了AbstractErrorWebExceptionHandler類,可以用于處理全局錯(cuò)誤時(shí)進(jìn)行自定義擴(kuò)展:
@Component@Order(-2)public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { // constructors @Override protected RouterFunction<ServerResponse> getRoutingFunction( ErrorAttributes errorAttributes) { return RouterFunctions.route( RequestPredicates.all(), this::renderErrorResponse); } private Mono<ServerResponse> renderErrorResponse( ServerRequest request) { Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); return ServerResponse.status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(errorPropertiesMap)); }}
我們將全局錯(cuò)誤處理的順序級(jí)別設(shè)置為-2,是為了賦予比DefaultErrorWebExceptionHandler更高的優(yōu)先級(jí),后者是@Order(-1)。
結(jié)論模塊化封裝一般需要包含全局異常處理進(jìn)行兜底,應(yīng)用服務(wù)自身可根據(jù)業(yè)務(wù)場(chǎng)景進(jìn)行路由函數(shù)級(jí)別的異常處理。
以上就是關(guān)于刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤,Spring WebFlux響應(yīng)式編程之異常處理的知識(shí),后面我們會(huì)繼續(xù)為大家整理關(guān)于刷卡機(jī)刷卡出現(xiàn)響應(yīng)碼錯(cuò)誤的知識(shí),希望能夠幫助到大家!
轉(zhuǎn)載請(qǐng)帶上網(wǎng)址:http://www.fzog.com.cn/shuaka/61140.html
- 上一篇:招商銀行有私人刷卡機(jī)嗎
- 下一篇:刷卡機(jī)二維碼收款怎么開(kāi)通