最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot webflux 過濾器(使用RouterFunction實現(xiàn))

 更新時間:2022年03月09日 15:04:38   作者:o_瓜田李下_o  
這篇文章主要介紹了springboot webflux 過濾器(使用RouterFunction實現(xiàn)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

webflux過濾器(RouterFunction實現(xiàn))

相關(guān)類與接口

HandlerFiterFunction

@FunctionalInterface
public interface HandlerFilterFunction<T extends ServerResponse, R extends ServerResponse> {
? ? Mono<R> filter(ServerRequest var1, HandlerFunction<T> var2);
?
? ? default HandlerFilterFunction<T, R> andThen(HandlerFilterFunction<T, T> after) {
? ? ? ? Assert.notNull(after, "HandlerFilterFunction must not be null");
? ? ? ? return (request, next) -> {
? ? ? ? ? ? HandlerFunction<T> nextHandler = (handlerRequest) -> {
? ? ? ? ? ? ? ? return after.filter(handlerRequest, next);
? ? ? ? ? ? };
? ? ? ? ? ? return this.filter(request, nextHandler);
? ? ? ? };
? ? }
?
? ? default HandlerFunction<R> apply(HandlerFunction<T> handler) {
? ? ? ? Assert.notNull(handler, "HandlerFunction must not be null");
? ? ? ? return (request) -> {
? ? ? ? ? ? return this.filter(request, handler);
? ? ? ? };
? ? }
?
? ? static HandlerFilterFunction<?, ?> ofRequestProcessor(Function<ServerRequest, Mono<ServerRequest>> requestProcessor) {
? ? ? ? Assert.notNull(requestProcessor, "Function must not be null");
? ? ? ? return (request, next) -> {
? ? ? ? ? ? Mono var10000 = (Mono)requestProcessor.apply(request);
? ? ? ? ? ? next.getClass();
? ? ? ? ? ? return var10000.flatMap(next::handle);
? ? ? ? };
? ? }
?
? ? static <T extends ServerResponse, R extends ServerResponse> HandlerFilterFunction<T, R> ofResponseProcessor(Function<T, Mono<R>> responseProcessor) {
? ? ? ? Assert.notNull(responseProcessor, "Function must not be null");
? ? ? ? return (request, next) -> {
? ? ? ? ? ? return next.handle(request).flatMap(responseProcessor);
? ? ? ? };
? ? }
}

HandlerFunction

@FunctionalInterface
public interface HandlerFunction<T extends ServerResponse> {
? ? Mono<T> handle(ServerRequest var1);
}

示例

config 層

CustomRouterConfig

@Configuration
public class CustomRouterConfig {
?
? ? @Bean
? ? public RouterFunction<ServerResponse> initRouterFunction(){
? ? ? ? return RouterFunctions.route()
? ? ? ? ? ? ? ? .GET("/test/**",serverRequest -> {
? ? ? ? ? ? ? ? ? ? System.out.println("path:"+serverRequest.exchange().getRequest().getPath().pathWithinApplication().value());
?
? ? ? ? ? ? ? ? ? ? return ServerResponse.ok().bodyValue("hello world");
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .filter((serverRequest, handlerFunction) -> {
? ? ? ? ? ? ? ? ? ? System.out.println("custom filter");
?
? ? ? ? ? ? ? ? ? ? return handlerFunction.handle(serverRequest);
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .build();
? ? }
}

使用測試

localhost:8080/test/text,控制臺輸出:

2020-06-21 15:18:08.005  INFO 16336 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2020-06-21 15:18:08.018  INFO 16336 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.807 seconds (JVM running for 2.641)
custom filter
path:/test/text

RouterFunction的webflux

RouterFunction可以運行在servlet或netty上,所以我們需要將兩個容器間的不同點抽象出來。

整個開發(fā)過程有幾步:

1.HandleFunction,實現(xiàn)輸入ServerRequest,輸出ServerResponse

2.RouterFunction,把請求url和HandlerFunction對應(yīng)起來

3.把RouterFunction包裝成HttpHandler,交給容器Server處理。

代碼

實體類和倉庫不變

handler:

@Component
public class UserHandler {
? ? private final UserRepository repository;
?
? ? public UserHandler(UserRepository repository) {
? ? ? ? this.repository = repository;
? ? }
?
? ? public Mono<ServerResponse> getAllUser(ServerRequest request){
? ? ? ? return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
? ? ? ? ? ? ? ? .body(repository.findAll() , User.class);
? ? }
? ? public Mono<ServerResponse> createUser(ServerRequest request){
? ? ? ? Mono<User> userMono = request.bodyToMono(User.class);
? ? ? ? return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
? ? ? ? ? ? ? ? .body(repository.saveAll(userMono) , User.class);
? ? }
? ? public Mono<ServerResponse> deleteUserById(ServerRequest request){
? ? ? ? String id = request.pathVariable("id");
? ? ? ? return this.repository.findById(id)
? ? ? ? ? ? ? ? .flatMap(user -> this.repository.delete(user)
? ? ? ? ? ? ? ? ? ? ? ? .then(ServerResponse.ok().build()))
? ? ? ? ? ? ? ? .switchIfEmpty(ServerResponse.notFound().build());
? ? }
}

router:

@Configuration
public class AllRouters {
? ? @Bean
? ? RouterFunction<ServerResponse> userRouter(UserHandler handler){
? ? ? ? return RouterFunctions.nest(
? ? ? ? ? ? ? ? //相當(dāng)于requestMapping
? ? ? ? ? ? ? ? RequestPredicates.path("/user") ,
? ? ? ? ? ? ? ? RouterFunctions.route(RequestPredicates.GET("/") , handler::getAllUser)
? ? ? ? ? ? ? ? ? ? .andRoute(RequestPredicates.POST("/").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)) , handler::createUser)
? ? ? ? ? ? ? ? ? ? .andRoute(RequestPredicates.DELETE("/{id}") , handler::deleteUserById));
?
? ? }
}

接下來看看routerFunction下的參數(shù)校驗

改造下代碼(這里只寫一個做例子)

public Mono<ServerResponse> createUser(ServerRequest request){
? ? ? ? Mono<User> userMono = request.bodyToMono(User.class);
? ? ? ? return userMono.flatMap(user -> {
? ? ? ? ? ? //在這里做校驗
? ? ? ? ? ? //xxx
? ? ? ? ? ? return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
? ? ? ? ? ? ? ? ? ? .body(repository.saveAll(userMono) , User.class);
? ? ? ? });
? ? }

異常捕獲,用aop的方式:

@Component
@Order(-99)
public class ExceptionHandler implements WebExceptionHandler {
? ? @Override
? ? public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
? ? ? ? ServerHttpResponse response = serverWebExchange.getResponse();
? ? ? ? response.setStatusCode(HttpStatus.BAD_REQUEST);
? ? ? ? response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
? ? ? ? String errorMsg = toStr(throwable);
? ? ? ? DataBuffer db = response.bufferFactory().wrap(errorMsg.getBytes());
? ? ? ? return response.writeWith(Mono.just(db));
? ? }
?
? ? private String toStr(Throwable throwable) {
? ? ? ? //已知異常,自定義異常,這里懶得寫了,就隨便找一個代替
? ? ? ? if (throwable instanceof NumberFormatException){
? ? ? ? ? ? NumberFormatException e = (NumberFormatException) throwable;
? ? ? ? ? ? return e.getMessage();
? ? ? ? }
? ? ? ? //未知異常
? ? ? ? else {
? ? ? ? ? ? throwable.printStackTrace();
? ? ? ? ? ? return throwable.toString();
? ? ? ? }
? ? }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot實現(xiàn)配置文件自動加載和刷新的示例詳解

    SpringBoot實現(xiàn)配置文件自動加載和刷新的示例詳解

    在使用Spring Boot開發(fā)應(yīng)用程序時,配置文件是非常重要的組成部分,在不同的環(huán)境中,我們可能需要使用不同的配置文件,當(dāng)我們更改配置文件時,我們希望應(yīng)用程序能夠自動加載和刷新配置文件,本文我們將探討Spring Boot如何實現(xiàn)配置文件的自動加載和刷新
    2023-08-08
  • 解決IDEA項目project包目錄消失的問題

    解決IDEA項目project包目錄消失的問題

    這篇文章主要介紹了解決IDEA項目project包目錄消失的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式

    SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式

    這篇文章主要介紹了SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用詳解

    SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用詳解

    本文主要介紹了SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 徹底搞懂Java多線程(一)

    徹底搞懂Java多線程(一)

    這篇文章主要給大家介紹了關(guān)于Java面試題之多線程和高并發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Springboot整合SpringSecurity的完整案例詳解

    Springboot整合SpringSecurity的完整案例詳解

    Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問控制解決方案的框架,Spring Security登錄認證主要涉及兩個重要的接口 UserDetailService和UserDetails接口,本文對Springboot整合SpringSecurity過程給大家介紹的非常詳細,需要的朋友參考下吧
    2024-01-01
  • SpringBoot整合MybatisPlus的基本應(yīng)用詳解

    SpringBoot整合MybatisPlus的基本應(yīng)用詳解

    MyBatis-Plus (簡稱 MP)是一個 MyBatis的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為 簡化開發(fā)、提高效率而生,本文將給大家介紹一下SpringBoot整合MybatisPlus的基本應(yīng)用,需要的朋友可以參考下
    2024-05-05
  • Mybatis有查詢結(jié)果但存不進實體類的解決方案

    Mybatis有查詢結(jié)果但存不進實體類的解決方案

    這篇文章主要介紹了Mybatis有查詢結(jié)果但存不進實體類的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java鎖競爭導(dǎo)致sql慢日志原因分析

    Java鎖競爭導(dǎo)致sql慢日志原因分析

    這篇文章主要介紹了Java鎖競爭導(dǎo)致sql慢的日志原因分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • java使用@Scheduled注解執(zhí)行定時任務(wù)

    java使用@Scheduled注解執(zhí)行定時任務(wù)

    這篇文章主要給大家介紹了關(guān)于java使用@Scheduled注解執(zhí)行定時任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論

宜君县| 织金县| 辽阳市| 九江县| 涿州市| 苍山县| 西藏| 宜兰市| 青川县| 英德市| 大姚县| 察雅县| 青河县| 达孜县| 敦煌市| 沐川县| 许昌市| 聊城市| 和顺县| 桃江县| 卢氏县| 盐亭县| 临沂市| 潢川县| 左云县| 伊川县| 吴旗县| 万安县| 繁昌县| 乳山市| 延安市| 韶山市| 陇川县| 内丘县| 建平县| 吴江市| 柳林县| 原平市| 福贡县| 宁德市| 伊春市|