简体   繁体   中英

How to get which endpoint operation is called from the HttpServletRequest in java?

I am using a Filter to detect some api operations and do some validation work. what is the normal way to get which api operation is currently called using HttpServletRequest? Right now, I am doing something like this:

 if (httpRequest.getMethod().equalsIgnoreCase("GET") 
       && httpRequest.getRequestURI().toLowerCase().contains("expectedOperatinName")) 

to check if it is the expected api call. However,

  1. it is not a smart way to do that to me
  2. more importantly, how does it distinguish btw list and get a single

Please help. Thanks

Did you look into the UriInfo interface that has methods on which resource matched the passed in request? You can access the UriInfo from the request like this:

containerRequestContext.getUriInfo()

I think you can easily find out which operation was honored using getMatchedResources() on the UriInfo. If you are trying to find the exact method that honored the request, look into UriRoutingContext class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM