我必须使用Spring Webflux以反应方式保存一些值。但是,当我发送请求时,会返回404状态作为响应。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
@RestController
@RequestMapping("/emp")
public class EmpController {
private EmployeeRepository empRepo;
@Autowired
public EmpController(EmployeeRepository empRepo)
{
this.empRepo=empRepo;
}
@PostMapping("/save")
@Consumes({MediaType.APPLICATION_JSON})
public void saveEmp(@RequestBody Mono<Employee> emp)
{
emp.subscribe(e-> {
e.setDate(new Date());
empRepo.saveEmp(e);
});
}
}
当我通过PostMan发送请求时,将返回404(未找到)。
JAX-RS
是Java EE
有关如何编写REST api的规范。然后,几个库已经实现了所述规范,例如Like Jersey
或restEasy
。在构建Java EE应用程序时,您需要这些库之一才能构建rest api。
Spring built their own way of building rest apis spring-web
for non reactive applications, and spring-webflux
for reactive applications.
Jersey
and restEasy
(to my knowledge) only works if you are building a non-reactive application.
In order to make your code work you need to remove:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
Jersey
is a Java EE implementation of JAX-RS
. It is used in Java EE to build Java EE styled rest apis. You are building a reactive application, using Spring Webflux which has its own way of building REST api's.
Spring is not a Java EE application. When you added that dependency, spring assumed that you wanted to build a Spring Application but not use the Spring built in REST api functions and annotations etc, so it didn't register your code, that you have written with Springs way of building rest apis.
假设您要以“球衣方式”编写REST api,如果您使用的是jersey,则需要手动注册api类。并且(据我所知)Jersey仅适用于非webflux应用程序。
这都是主要的基础知识,如果您不理解为什么我建议您在尝试webflux之前阅读并构建常规的Spring Boot应用程序。
我建议您阅读以下部分:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句