Я только недавно начал играть с реактивным веб-стеком, используя Spring-boot starter webflux, и у меня возникли проблемы с очень простым RestController в проекте.
Вот необходимые части головоломки:
пом.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";
}
}
Я установил следующие свойства в приложение.свойства
server.port=8911
server.servlet.context-path=/sample-reactive
Теперь, когда я запускаю приложение как приложение Spring Boot через Eclipse и выполняю запрос 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)
Любые идеи о том, почему это происходит, потому что согласно документации я понимаю, что можно использовать аннотации @RestController даже с Webflux.
@Mr.J4mes Нет, я все еще вижу ту же проблему.
context-path
поддерживается для webflux: https://github.com/spring-projects/spring-boot/issues/14082
Спасибо, я наткнулся на тот же пост. REST API без проблем вызывает всю работу, если я не укажу контекстный путь. Я, вероятно, просто добавлю контекстный путь в качестве значения в аннотацию @requestmapping, поскольку имеет смысл, почему webflux не поддерживает понятие контекстного пути.
Попробуйте заменить заголовок Content-Type на application/json?