Search code examples
javaspringspring-mvcspring-restcontrollerrequest-mapping

RequestMapping inheritence between class level and method level


Given the @Controller below, even if i send a Get Request to the webApp, the controller run the homePage method.

@RestController
@RequestMapping(method = RequestMethod.POST)
public class MyController {

        @GetMapping("/hello")
        public String homePage() {
            return "Hello, It is my first application";
        }
    }

How could that happen? Normally, i restrict that from the class level.


Solution

  • Your method with @GetMapping("/hello") picked up as most specific and enables GET requests with /hello path

    This annotation can be used both at the class and at the method level. In most cases, at the method level applications will prefer to use one of the HTTP method specific variants @GetMapping