Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty @RequestMapping method should match both "" and "/" consistently #30293

Closed
bclozel opened this issue Apr 6, 2023 · 3 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@bclozel
Copy link
Member

bclozel commented Apr 6, 2023

See spring-projects/spring-boot#33499 for initial report. It looks like the enhancement in #29625 was incomplete.

The goal was to make the following work for both http://localhost:8080 and http://localhost:8080/:

@RestController
public class HomeController {

    @GetMapping
    public String home() {
        return "Hello, World!";
    }

}

But this use case doesn't work still, as the fix was focusing on the combining of @RequestMapping annotations. In the code snippet above, org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition#combine is never called as org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#getMappingForMethod will only call it if the RequestMappingInfo is available on the type. Here, it's null so the combination never happens.

#29625 did change the following, when the controller type itself is annotated with @RequestMapping.

@RestController
@RequestMapping
public class HomeController {

    @GetMapping
    public String home() {
        return "Hello, World!";
    }

}
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Apr 6, 2023
@bclozel bclozel added this to the 6.0.x milestone Apr 6, 2023
@BrijeshPatra
Copy link

BrijeshPatra commented Apr 8, 2023

if you want a method annotated with @RequestMapping to match both "" (empty path) and "/" (root path), you can specify both values in the value or path attribute of the annotation, separated by a comma. For example:

@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public String myMethod() {
// method implementation
}
In the above example, the myMethod method will handle requests to both the empty path and the root path.

@bclozel
Copy link
Member Author

bclozel commented Apr 8, 2023

@BrijeshPatra thanks but this is not a question, but rather a behavior change that the team wants to make.

@BrijeshPatra
Copy link

BrijeshPatra commented Apr 8, 2023

Oh okay! Thanks for your reply actually i want to contribute in this project can you please guide me here.
@bclozel

@rstoyanchev rstoyanchev modified the milestones: 6.0.x, 6.0.10 Jun 12, 2023
@rstoyanchev rstoyanchev changed the title @RequestMapping annotated methods should match both "" and "/" Empty @RequestMapping method should match both "" and "/" consistenty Jun 12, 2023
@rstoyanchev rstoyanchev changed the title Empty @RequestMapping method should match both "" and "/" consistenty Empty @RequestMapping method should match both "" and "/" consistently Jun 12, 2023
@rstoyanchev rstoyanchev added type: bug A general bug and removed type: enhancement A general enhancement labels Jun 12, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants