Spring rest controller got 404 error code in Spring boot 3.0.3
Asked Answered
N

3

5

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?

Normand answered 26/2, 2023 at 12:53 Comment(0)
H
9

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

Hypothecate answered 6/6, 2023 at 5:16 Comment(0)
D
5

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.

Dative answered 4/3, 2023 at 12:41 Comment(0)
T
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.

Tomfool answered 2/3, 2023 at 20:51 Comment(1)
I can confirm this. I had the same issue and downgraded to 3.0.2. The controller was then scanned properly.Legitimate

© 2022 - 2024 — McMap. All rights reserved.