spring-boot-webflux 基本使用

pom引入依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

代码

package org.qx.web.router;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.web.servlet.function.EntityResponse.fromObject;
import static org.springframework.web.servlet.function.ServerResponse.ok;

@Configuration
public class MyRouters {
    @Bean
    public RouterFunction<ServerResponse> helloRoutesV1() {
        return RouterFunctions.route(RequestPredicates.path("/v1/hello-world"),
                request -> ok().body(fromObject("Hello World v1!")));
    }

    @Bean
    public RouterFunction<ServerResponse> helloRoutesV2() {
        return RouterFunctions.route(RequestPredicates.path("/v2/hello-world"),
                request -> ok().body(fromObject("Hello World v2!!!")));
    }

}

异常情况

Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider

解决:

引入pom依赖

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
</dependency>

 

解析:

其实用的是routerFunctionMapping 去进行路由解析的

 

posted @ 2022-03-12 13:23  浅笑19  阅读(570)  评论(0)    收藏  举报