Spring Rest 控制器在 Spring boot 3.0.3 中收到 404 错误代码

rez*_*tin 3 spring spring-mvc spring-boot

有一个示例 Spring Boot(版本 3.0.3)Web 应用程序如下:

pom.xml部分

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <start-class>matin.example.demo.DemoApplication</start-class>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

package matin.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

package matin.example.demo.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class FController {
    @GetMapping(value = "/re")
    public String viewHomePage(/*Model model, HttpServletRequest request*/) {
        /*request.getSession().setAttribute("onlineUser", request.getSession().getAttribute("onlineUser") == null ? 1 : Integer.valueOf(request.getSession().getAttribute("onlineUser").toString()) + 1);
        System.out.println(request.getSession().getAttribute("onlineUser") + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");*/
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

当我请求 as 时localhost:8080/re,我收到 404 not found 错误,但是当我将 Spring Boot 版本降级到 2.7.7 时,它工作成功。

3.x版本中哪里发生了变化,我该怎么办?

Bri*_*zel 5

您可能会遇到这个 Spring 框架问题:如果路径包含空格,则扫描 Bean 将无法正常工作。这种情况尤其发生在 Windows 环境中工作的人身上。

升级到 Spring Boot 3.0.4(其中包含相应的 Spring 框架升级)后,您应该会看到问题消失。


Pau*_*aul 5

SpringBoot 3.x 以及 Spring 6.x 更改了它们的 URL 匹配。我正在进行一个旧项目升级,其中微服务使用包含尾部斜杠的 URL 来访问我们的控制器,导致 404 NOT FOUND。

(这似乎与您的问题类似,尽管您没有尾部斜杠,我认为这可能是一个拼写错误?)

可以通过配置以下内容来修复此问题,这将导致两个 URL 都正常工作:localhost:8080/users以及localhost:8080/users/

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
      configurer.setUseTrailingSlashMatch(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住,此方法已被弃用!来自 Spring 文档本身:“...从 6.0 开始,不推荐使用对尾部斜杠的透明支持,转而支持通过代理、Servlet/Web 过滤器或控制器配置显式重定向。”

其他来源: Spring Boot 3 中的 URL 匹配


归档时间:

查看次数:

3755 次

最近记录:

2 年,2 月 前