How to know which param of @RequestMapping is called

后端 未结 3 1453
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 09:48

This is my @RequestMapping annotation:

  @RequestMapping({\"/loginBadCredentials\", \"/loginUserDisabled\", \"/loginUserNumberExceeded\"})
  public          


        
相关标签:
3条回答
  • 2021-01-22 10:19

    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.

    0 讨论(0)
  • 2021-01-22 10:21

    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
    }
    
    0 讨论(0)
  • 2021-01-22 10:34

    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
      }
    
    0 讨论(0)
提交回复
热议问题