Spring Boot无法注册组件并返回404

时间:2019-03-22 02:26:32

标签: java spring spring-boot

我对Spring Boot响应式api很陌生,我不明白为什么我的控制器被注册了。

项目树

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("hello.reactive.api")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Application.java

@Configuration
public class GreetingRouter {

    @Bean
    public RouterFunction<ServerResponse> route(GreetingHandler greetingHandler) {

    return RouterFunctions
        .route(RequestPredicates.GET("/hello").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), greetingHandler::hello);
}

GreetingRouter.java

{{1}}

当我运行应用程序并尝试http://localhost:8080/hello时,始终返回以下错误页面:

此应用程序没有针对/ error的显式映射,因此您将其视为备用。

请发表任何评论以帮助解决此问题。.

1 个答案:

答案 0 :(得分:0)

在pom中使用此依赖项-> https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/2.1.3.RELEASE 并在您的GreetingRouter中使用以下命令:

  @Controller
    @RequestMapping("/api")
    public class GreetingRouter {

    @GetMapping("/hello")
    public ResponseEntuity<ServerResponse> route(GreetingHandler greetingHandler) {

    RouterFunctions router = new RouterFunctions.route(RequestPredicates.GET("/hello").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), greetingHandler::hello);
    return new ResponseEntity<ServerResponse>(router, HttpResponse.OK);
}