Spring Boot Webflux无法解析RestController

时间:2019-04-10 00:20:09

标签: java maven spring-boot spring-webflux

最近,我刚刚开始使用Spring-boot启动程序webflux来处理反应式Web堆栈,并且在项目中使用非常简单的RestController遇到了问题。

这是拼图中必不可少的部分:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hello.sample</groupId>
    <artifactId>sample-reactive-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sample-reactive-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

SampleController.java

package com.hello.sample.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController
{

    @GetMapping("/hello")
    public String hello()
    {
        return "Hello WebFlux";
    }
}

我在 application.properties

中设置了以下属性
server.port=8911
server.servlet.context-path=/sample-reactive

现在,当我通过Eclipse作为Spring Boot应用程序运行该应用程序并执行curl或Postman请求时,我得到了404  :enter image description here

查看控制台日志,我看不到已定义的requestmapping绑定任何请求映射。

这是控制台日志   。 ____ _ __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-09 17:18:40.835  INFO 59587 --- [           main] i.n.d.s.SampleReactiveServiceApplication : Starting SampleReactiveServiceApplication on C02NJ38EG3QP-2.local with PID 59587 (/Users/developer/Documents/navajoWorkspace/sample-reactive-service/target/classes started by developer in /Users/developer/Documents/navajoWorkspace/sample-reactive-service)
2019-04-09 17:18:40.837  INFO 59587 --- [           main] i.n.d.s.SampleReactiveServiceApplication : No active profile set, falling back to default profiles: default
2019-04-09 17:18:41.631  INFO 59587 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8911
2019-04-09 17:18:41.634  INFO 59587 --- [           main] i.n.d.s.SampleReactiveServiceApplication : Started SampleReactiveServiceApplication in 0.972 seconds (JVM running for 1.527)

为什么会发生这种情况的任何想法,因为据文档了解,即使使用Webflux,也可以使用 @RestController 批注

1 个答案:

答案 0 :(得分:1)