How do i get the requestmapping value in the controller?

后端 未结 5 2072
名媛妹妹
名媛妹妹 2021-02-05 04:43

In the controller , i have this code, somehow, i want to get the request Mapping value \"search\". How is it possible ?

 @RequestMapping(\"/search/\")     
 publ         


        
相关标签:
5条回答
  • 2021-02-05 05:11

    For Spring 3.1 and above you can use ServletUriComponentsBuilder

    @RequestMapping("/search/")     
         public ResponseEntity<?> searchWithSearchTerm(@RequestParam("name") String name) {  
             UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
             System.out.println(builder.buildAndExpand().getPath());
             return new ResponseEntity<String>("OK", HttpStatus.OK);
         }
    
    0 讨论(0)
  • 2021-02-05 05:12

    If you want the pattern, you can try HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE:

    @RequestMapping({"/search/{subpath}/other", "/find/other/{subpath}"})
    public Map searchWithSearchTerm(@PathVariable("subpath") String subpath,
                                                 @RequestParam("name") String name) {
    
        String pattern = (String) request.getAttribute(
                                     HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        // pattern will be either "/search/{subpath}/other" or
        // "/find/other/{subpath}", depending on the url requested
        System.out.println("Pattern matched: "+pattern);
    
    }
    
    0 讨论(0)
  • 2021-02-05 05:13

    Having a controller like

    @Controller
    @RequestMapping(value = "/web/objet")
    public class TestController {
    
        @RequestMapping(value = "/save")
        public String save(...) {
            ....
        }
    }
    

    You cant get the controller base requestMapping using reflection

    // Controller requestMapping
    String controllerMapping = this.getClass().getAnnotation(RequestMapping.class).value()[0];
    

    or the method requestMapping (from inside a method) with reflection too

    //Method requestMapping
    String methodMapping = new Object(){}.getClass().getEnclosingMethod().getAnnotation(RequestMapping.class).value()[0];
    

    Obviously works with an in requestMapping single value.

    Hope this helps.

    0 讨论(0)
  • 2021-02-05 05:31
    @RequestMapping("foo/bar/blub")     
    public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {
      // delivers the path without context root 
      // mapping = "/foo/bar/blub"
      String mapping = request.getPathInfo();
      // more code here
    }
    
    0 讨论(0)
  • 2021-02-05 05:33

    It seems you are looking for the path that this request has matched, then you can directly get it from servlet path

    @RequestMapping("/search/")     
     public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {    
    String path = request.getServletPath();
            // more code here     
     }
    
    0 讨论(0)
提交回复
热议问题