build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
同时引入spring-boot-starter-web和spring-boot-starter-webflux 会导致Handler 404
handler
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class TestHandler {
public Mono<ServerResponse> get(ServerRequest serverRequest) {
return ServerResponse.ok().body(Mono.just("name"), String.class);
}
}
config 配置路由
import com.example.webfluxdemo.controller.TestHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class HandleConfig {
@Autowired
private TestHandler testHandler;
@Bean
public RouterFunction<ServerResponse> userRouter() {
return RouterFunctions.route().GET("/test", testHandler::get).build();
}
}
启动项目

请求接口


本文探讨了在使用Spring Boot时,由于同时引入spring-boot-starter-web和spring-boot-starter-webflux,导致Handler 404错误的情况。通过分析build.gradle文件、配置路由及启动项目的步骤,提出了问题的解决方案。

1万+

被折叠的 条评论
为什么被折叠?



