Spring REST控制器在Spring Boot 3.0.3中收到了404错误代码。

huangapple go评论54阅读模式
英文:

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

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

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

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 = &quot;/re&quot;)
    public String viewHomePage(/*Model model, HttpServletRequest request*/) {
        /*request.getSession().setAttribute(&quot;onlineUser&quot;, request.getSession().getAttribute(&quot;onlineUser&quot;) == null ? 1 : Integer.valueOf(request.getSession().getAttribute(&quot;onlineUser&quot;).toString()) + 1);
        System.out.println(request.getSession().getAttribute(&quot;onlineUser&quot;) + &quot;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;);*/
        return &quot;index&quot;;
    }
}

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/userslocalhost: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

huangapple
  • 本文由 发表于 2023年2月26日 20:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定