响应式编程(reactive programming)是一种基于数据流(data stream)和变化传递(propagation of change)的声明式(declarative)的编程范式
在命令式编程(咱们的平常编程模式)下,式子a=b+c,这就意味着a的值是由b和c计算出来的。若是b或者c后续有变化,不会影响到a的值
在响应式编程下,式子a:=b+c,这就意味着a的值是由b和c计算出来的。但若是b或者c的值后续有变化,会影响到a的值
我认为上面的例子已经能够帮助咱们理解变化传递(propagation of change)react
lambda:
函数式编程接口:
@FunctionalInterface
其中在函数式接口中有一些只适合在函数式中使用的官方封装好的接口方法
如BiFunction,Tuple
BiFunction适用于两个参数,而Function适用于一个参数
Function的使用:编程
/** * 一个参数的调用返回 * * @param option * @param funOptionService * @return */ public static Mono<ServerResponse> oneOptionalParams(Optional<String> option, Function<Optional<String>, Mono<JSONObject>> funOptionService, String judgeParam) { //option is not Present and the judgeParams not null then badRequest if (!option.isPresent() && (null != judgeParam) && judgeParam != "") { return (Mono<ServerResponse>) ServerResponseBuild.badRequestMsg("参数" + judgeParam + "不能为空"); } Mono<JSONObject> jsonObjectMono = funOptionService.apply(option); return jsonObjectMono.flatMap( resultJson -> { if (resultJson.getInteger("errcode") != 0) { return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg")); } log.info("\n返回数据:{}", resultJson); return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class); } ).switchIfEmpty(ServerResponseBuild.badRequestMsg("请求错误") ).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage())); }
BiFunction两个参数的json
/** * 两个参数的调用返回 * @param option1 * @param option2 * @param funBiOptionService * @param judgeParams * @return */ public static Mono<ServerResponse> twoOptionalParams(Optional<String> option1, Optional<String> option2, BiFunction<Optional<String>, Optional<String>, Mono<JSONObject>> funBiOptionService, String... judgeParams) { //省略judgeParams... Mono<JSONObject> jsonObjectMono = null; try { jsonObjectMono = funBiOptionService.apply(option1, option2); } catch (Exception e) { return Mono.error(e); } return jsonObjectMono.flatMap( resultJson -> { if (resultJson.getInteger("errcode") != 0) { return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg")); } log.info("\n返回数据:{}", resultJson); return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class); } ).switchIfEmpty(ServerResponseBuild.badRequestMsg("请求错误") ).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage())); }
函数式调用
这样写起来确实看上去简洁了许多
且这样调用能够调用接收多个参数
一个参数:app
/** * 获取访问用户身份 * * @param request * @return */ public Mono<ServerResponse> getuserInfo(ServerRequest request) { return ServerResponseBuild.oneOptionalParams(request.queryParam("code"), option -> manIdentityVerifyService.getUserInfo(option), "code"); }
上面的写法能够改成:函数式编程
/** * 获取访问用户身份 * * @param request * @return */ public Mono<ServerResponse> getuserInfo(ServerRequest request) { return ServerResponseBuild.oneOptionalParams(request.queryParam("code"), manIdentityVerifyService::getUserInfo, "code"); }
两个参数的一样可使用::方式来调用方法函数
/** * ~~~~不给你看注释 * @return */ public Mono<ServerResponse> getConversations(ServerRequest request) { return ServerResponseBuild.twoOptionalParams( request.queryParam("seq"), request.queryParam("limit"), chatMsgSaveService::monoGetChatContent, "seq", "limit"); }