Spring Boot 3.0.3 中的 Spring REST 控制器出现了 404 错误代码。

3

有一个类似于这样的示例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>

并且

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);
    }

}

并且

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";
    }
}
当我请求localhost:8080/re时,我收到了404未找到的错误,但是当我将Spring Boot的版本降级为2.7.7时,它成功地工作了。 在3.x版本中发生了什么变化,我该怎么做?
4个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
4
您可能遇到了 Spring Framework 的问题,如果路径中包含空格,则无法扫描 bean。这种情况尤其发生在使用 Windows 环境的人身上。 升级到包含适当的 Spring Framework 升级版本的 Spring Boot 3.0.4 后,您应该可以解决此问题。

2

我曾遇到类似的问题,通过将pom.xml中的"spring-boot-starter-parent"版本从"3.0.3"更改为"3.0.2"来解决。不确定这是否是发布版本之间的变化或新版本中的错误。


1
我可以确认这一点。我曾经遇到过同样的问题,然后降级到了3.0.2版本。控制器随后被正确扫描了。 - jouhami

2

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);
    }
}
请注意,此方法已被弃用!来自Spring文档本身的说明:'...自6.0版本起,对尾随斜杠的不透明支持已被弃用,而应通过代理、Servlet/web过滤器或控制器配置显式重定向。'

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


-2
尝试在结尾添加 /,即 "localhost:8080/re/"。

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