Spring Webflux返回404(未找到)错误。

3
我需要使用Spring Webflux以响应式的方式保存一些值。但是当我发送请求时,会返回404状态作为响应。

pom.xml

 <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>

EmpController类

@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(未找到)错误。

enter image description here enter image description here


请确保您的EmpController位于@ComponentScan下。如果您没有放置任何@ComponentScan,请确保您已将文件放置在与main()所在的相同或子包中。您可以在此处共享您的项目结构以便更好地理解,或者创建一个GitHub存储库并分享URL。 - undefined
1
@Vipul Kumar,这里是 Github 仓库链接:https://github.com/Benzeman97/reactive-async-app.git - undefined
不需要 jersey 依赖。Spring 不使用 jersey。 - undefined
在删除jersey之后,它起作用了@ThomasAndolf。如果可能的话,请告诉我原因。 - undefined
4个回答

2

我遇到了同样的问题。 解决方案:

  1. 打开 application.properties 文件,
  2. 删除 server.servlet.context-path 配置项,
  3. 添加 spring.webflux.base-path 配置项。

是的,对我来说就是这个问题!我正在将一个非响应式的应用程序升级为响应式,这就是问题所在!谢谢你的分享!:D - undefined

2

JAX-RS是Java EE中编写REST API的规范。多个库已经实现了该规范,例如JerseyrestEasy。在构建Java EE应用程序时,您需要其中一个库才能构建REST API。

Spring构建了自己的构建REST API的方式:spring-web用于非响应式应用程序,而spring-webflux用于响应式应用程序。

JerseyrestEasy(据我所知)只适用于构建非响应式应用程序。

为使您的代码正常工作,您需要删除:

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

Jersey 是 JAX-RS 的 Java EE 实现。它用于在 Java EE 中构建 Java EE 风格的 REST API。您正在使用 Spring Webflux 构建反应式应用程序,它具有自己的构建 REST API 的方式。

Spring 不是一个 Java EE 应用程序。当您添加依赖项时,Spring 假定您要构建一个 Spring 应用程序,但不使用 Spring 内置的 REST API 函数和注释等,因此它没有注册您编写的代码,这些代码是使用 Spring 构建 REST API 的方式编写的。

它假设您将编写“Jersey方式”的 REST API,并且如果您正在使用 Jersey,则需要手动注册您的 API 类。而且(据我所知)Jersey 只适用于非 Webflux 应用程序。

这主要是基础知识,如果您不理解为什么,我建议您先阅读并构建常规的 Spring Boot 应用程序,然后再尝试使用 Webflux。

我建议您阅读以下部分:

响应式编程,Reactor 入门

Baeldung Webflux

构建反应式 Webflux 应用程序

Spring Boot Webflux


0

0

你需要移除以下的Jersey依赖。

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

原因是spring-boot-starter-jersey是用于使用JAX-RS和Jersey构建Restful Web应用程序的启动器。由于您在项目中使用了它,因此Spring不会使用内置的Spring函数来处理像@GetMapping、@PostMapping这样的rest api。
如果您想使用Jersey来创建rest api,则可以使用@GET注解来定义Get api,并使用@Produces来定义映射,如下所示。
例如:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.stereotype.Service;

@Service
@Path("/hello")
public class HelloService {

    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hello from Spring";
    }
}

还有,你需要在JerseyConfig中注册这个类。
import com.zetcode.endpoint.HelloService;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        
        register(HelloService.class);
    }
}

如果您想继续使用Spring的内置功能并使用反应式编程,只需删除Jersey依赖项,并使用WebFlux依赖项来创建您的REST API。


我甚至不确定Jersey是否可以用于响应式应用程序。你能确认一下吗? - undefined
是的,它不能用于响应式应用程序。对于响应式应用程序,我们必须使用WebFlux,并移除Jersey依赖。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接