2

This is my @RequestMapping annotation:

  @RequestMapping({"/loginBadCredentials", "/loginUserDisabled", "/loginUserNumberExceeded"})
  public String errorLogin(...){        
            ... 
        }

Inside the method errorLogin , is there a way to know which of the three url was "called"?

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
MDP
  • 4,177
  • 21
  • 63
  • 119

3 Answers3

3

Add HttpServletRequest as your parameters and use it to find the current request path.

Update: Spring also provides RequestContextHolder:

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String currentReqUri = attributes.getRequest().getRequestURI();

In my opinion, first approach is better and a little more testable.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • Thank you. Actually I already did it, but I was wondering if there was a specific "Spring way" to do it. – MDP Jan 21 '16 at 09:35
3

you can inject the HttpServletRequest into the method-parameters and then get the called uri.

  @RequestMapping({"/loginBadCredentials", "/loginUserDisabled", "/loginUserNumberExceeded"})
  public String errorLogin(HttpServletRequest request) {        
            String uri = request.getRequestURI(); 
            // do sth with the uri here
  }
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
  • Thank you. Actually I already did it, but I was wondering if there was a specific "Spring way" to do it. – MDP Jan 21 '16 at 09:35
1

Simplest method is to inject HttpServletRequest and get the uri:

@RequestMapping({"/loginBadCredentials", "/loginUserDisabled", "/loginUserNumberExceeded"})
public String errorLogin(HttpServletRequest request) {        
        String uri = request.getRequestURI(); 
        // switch on uri what you need to do
}
Placinta Alexandru
  • 463
  • 2
  • 7
  • 20
  • Thank you. Actually I already did it, but I was wondering if there was a specific "Spring way" to do it. – MDP Jan 21 '16 at 10:16