我最近刚刚开始使用 Spring-boot starter 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 : 。
查看控制台日志,我没有看到任何请求映射绑定(bind)到定义的请求映射。
这是控制台日志 。 ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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 注释
最佳答案
webflux 支持
context-path
:https://github.com/spring-projects/spring-boot/issues/14082
关于java - Spring boot webflux无法解析RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55603005/