In the controller , i have this code, somehow, i want to get the request Mapping value \"search\". How is it possible ?
@RequestMapping(\"/search/\")
publ
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);
}
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);
}
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.
@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
}
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
}