【问题标题】:Spring boot webflux unable to resolve RestControllerSpring boot webflux 无法解析 RestController
【发布时间】:2019-08-31 08:22:13
【问题描述】:

我最近刚开始使用 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 :

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

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

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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 注释

【问题讨论】:

  • 尝试用 application/json 替换 Content-Type 标头?
  • @Mr.J4mes 不,我仍然看到同样的问题

标签: java maven spring-boot spring-webflux


【解决方案1】:

【讨论】:

  • 谢谢我偶然发现了同一个帖子。如果我不指定上下文路径,REST API 调用所有工作都没有问题。我可能会在 @requestmapping 注释中添加上下文路径作为值,因为 webflux 不支持上下文路径的概念是有道理的
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2017-08-11
  • 2019-06-11
  • 2019-08-13
  • 2014-08-27
  • 2017-07-08
  • 2017-12-01
相关资源
最近更新 更多