springboot webflux 過濾器(使用RouterFunction實現(xiàn))
webflux過濾器(RouterFunction實現(xià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對應起來
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(
? ? ? ? ? ? ? ? //相當于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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot實現(xiàn)配置文件自動加載和刷新的示例詳解
在使用Spring Boot開發(fā)應用程序時,配置文件是非常重要的組成部分,在不同的環(huán)境中,我們可能需要使用不同的配置文件,當我們更改配置文件時,我們希望應用程序能夠自動加載和刷新配置文件,本文我們將探討Spring Boot如何實現(xiàn)配置文件的自動加載和刷新2023-08-08
SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式
這篇文章主要介紹了SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用詳解
本文主要介紹了SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Springboot整合SpringSecurity的完整案例詳解
Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問控制解決方案的框架,Spring Security登錄認證主要涉及兩個重要的接口 UserDetailService和UserDetails接口,本文對Springboot整合SpringSecurity過程給大家介紹的非常詳細,需要的朋友參考下吧2024-01-01
SpringBoot整合MybatisPlus的基本應用詳解
MyBatis-Plus (簡稱 MP)是一個 MyBatis的增強工具,在 MyBatis 的基礎上只做增強不做改變,為 簡化開發(fā)、提高效率而生,本文將給大家介紹一下SpringBoot整合MybatisPlus的基本應用,需要的朋友可以參考下2024-05-05

