Skip to content

@RequestMapping without any attributes behaves as "default endpoint" #22543

Closed
@anatoliy-balakirev

Description

@anatoliy-balakirev

Spring version: 5.1.5.RELEASE

In one of our micro services we've made a mistake and instead of writing something like this:

@RestController
@RequestMapping("/my/custom/controller")
public class MyController {

// GET and POST methods mapped to `/my/custom/controller` endpoint

Created this:

@RestController("/my/custom/controller")
public class MyController {

Everything worked fine, all our requests were handled but then at some point we realised that all requests, even for non-existing endpoints, are mapped to that controller. So this:

http://localhost:8080/whatever

Was getting into our controller by default:

2019-03-07 16:47:28.014 DEBUG 83179 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/whatever", parameters={}
2019-03-07 16:47:28.016 DEBUG 83179 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public void com.example.demo.MyController.doSomething()
Got to my custom controller 1
2019-03-07 16:47:28.017 DEBUG 83179 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2019-03-07 16:47:28.017 DEBUG 83179 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Nothing to write: null body
2019-03-07 16:47:28.017 DEBUG 83179 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

With that config in place, I would expect to get 404 for both: /whatever and /my/custom/controller requests, because in fact there is no mapping for /my/custom/controller in my app (there is a bean with that name instead).

Here is a whole controller's code, no other config is required to reproduce this, just standard spring boot app with this single controller:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/my/custom/controller")
public class MyController {

    @GetMapping
    public void doSomething() {
        System.out.println("Got to my custom controller 1");
    }

    @PutMapping
    public void doSomethingElse() {
        System.out.println("Got to my custom controller 2");
    }

}

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions