Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestMapping with multiple values with pathvariable - Spring 3.0

@RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method = RequestMethod.GET)
public String userDetails(Map Model,****) {
//what goes here? 
}

What will be my arguments to the userDetails method? And how do I differentiate /userDetails and /userDetails/edit/9 within the method?

like image 513
Aravind Vel Avatar asked Apr 09 '12 11:04

Aravind Vel


People also ask

How do I send a variable to multiple paths?

Try this: @RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod. GET) public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) { // Your code goes here. }

Which of the following is a method where RequestMapping is used with multiple URI?

@RequestMapping With Multiple URIs You can have multiple request mappings for a method. For that add one @RequestMapping annotation with a list of values. As you can see in this code, @RequestMapping supports wildcards and ant-style paths.

Can we use @RequestMapping with @component?

Quite right, you can only use @RequestMapping on @Controller annotated classes.


1 Answers

Ideally we can get pathvariable by using annotation @PathVariable in method argument but here you have used array of url {"/userDetails", "/userDetails/edit/{id}"} so this will give error while supply request like localhost:8080/domain_name/userDetails , in this case no id will be supplied to @PathVariable.

So you can get the difference (which request is comming through) by using argument HttpServletRequest request in method and use this request object as below -

String uri = request.getRequestURI();

Code is like this -

   @RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method=RequestMethod.GET)
   public String userDetails(Map Model,HttpServletRequest request) {
   String uri = request.getRequestURI();  
  //put the condition based on uri
 }
like image 89
kundan bora Avatar answered Oct 14 '22 05:10

kundan bora