英文:
Spring rest controller got 404 error code in Spring boot 3.0.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>
以及DemoApplication.java:
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);
}
}
还有FController.java:
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 Not Found错误,但是当我将Spring Boot的版本降级到2.7.7时,它成功工作。
问题出在3.x版本中的哪里,我该怎么做?
英文:
There is a sample spring boot(version 3.0.3) web application like this:
The part of 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>
and
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);
}
}
and
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";
}
}
when i request as localhost:8080/re
, i got 404 not found error, but when i downgrade the version of spring boot to 2.7.7, it is worked successfully.
Where is it changed in the version of 3.x and what do i do?
答案1
得分: 4
你可能遇到了这个Spring Framework问题,其中如果路径包含空格,则无法扫描bean。这特别发生在使用Windows环境的人身上。
在升级到包含适当Spring Framework升级的Spring Boot 3.0.4后,你应该看到该问题消失。
英文:
You are probably hitting this Spring Framework issue where scanning for beans was not working if the path contains spaces. This happened especially to people working in a Windows environment.
You should see the issue go away after upgrading to Spring Boot 3.0.4, which contains the appropriate Spring Framework upgrade.
答案2
得分: 2
我遇到了一个非常类似的问题,通过将pom.xml中的"spring-boot-starter-parent"版本从"3.0.3"更改为"3.0.2"来解决了它。不确定这是发布版本之间的更改还是较新版本的错误。
英文:
I had a very similar issue which I resolved by changing the version of "spring-boot-starter-parent" in pom.xml from "3.0.3" to "3.0.2". Not sure if this is a change between releases or a bug with the newer release.
答案3
得分: 2
SpringBoot 3.x 和随之而来的 Spring 6.x 已经更改了它们的 URL 匹配。我正在进行一个遗留项目的升级,在这个项目中,微服务使用包含尾随斜杠的 URL 来访问我们的 Controller,导致出现 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 匹配
英文:
SpringBoot 3.x and consequently Spring 6.x have changed their URL matching. I work on a legacy project upgrade where microservices were hitting our Controller with URL's containing a trailing slash, resulting in a 404 NOT FOUND.
(this seems similar to your question, although you don't have a trailing slash, which I assume could be a typo?)
This can be fixed by configuring the following, which will result in both URL's working: localhost:8080/users, as well as localhost:8080/users/
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(true);
}
}
Keep in mind, this method is deprecated! From the Spring docs themselves: '...Deprecated transparent support for trailing slashes is deprecated as of 6.0 in favor of configuring explicit redirects through a proxy, Servlet/web filter, or a controller.'
Other source: URL matching in Spring Boot 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论