Search code examples
javaspringrestspring-mvcrequest-mapping

How to ignore level @RequestMapping() and direct call method level @RequestMapping()?


How to do I ignore the class level @RequestMapping("/home") and directly call the method level @RequestMapping("/users") in Spring?

@Controller
@RequestMapping("/home")

@RequestMapping("/method1")
public void method1(){
...
}

@RequestMapping("/users")
public void listUsers(){
...
}

I want to call http://localhost:8080/users to invoke listUsers() method.


Solution

  • You cannot bypass requestmapping defined at class level. for If so why you want a class level mapping then... you can instead do something like this in the method level request mapping

    @Controller
    @RequestMapping("/home/method1")
    public void method1(){
    ...
    }
    
    @RequestMapping("/users")
    public void listUsers(){
    ...
    }
    

    In that case you may try this...

     @Controller
    @RequestMapping({ "/home", "/users" })
    
    
        @RequestMapping("/method1")
        public void method1(){
        ...
        }
    
       @RequestMapping(method="RequestMethod.GET")
        public void listUsers(){
        ...
        }