Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple @PathVariable in Spring MVC

Couldn't find an answer to this unfortunately so hoping someone can help.

In Spring MVC 3.1.0 here is my method:

@RequestMapping(value = "/{app}/conf/{fnm}", method=RequestMethod.GET)
public ResponseEntity<?> getConf(@PathVariable String app, @PathVariable String fnm) {
    log.debug("AppName:" + app);
    log.debug("fName:" + fnm);
            ...
            return ...
    }

I've seen some examples online and it appears there is no problem having multiple @PathVariables in theory.

However when I do it, both "app" and "fnm" contain the same value (which is whatever value was assigned to "app").

Really appreciate any insight someone may have to where I'm going wrong?

Thanks!

like image 567
user1389920 Avatar asked Jul 05 '12 19:07

user1389920


People also ask

Can we have two path variables?

4. Multiple Path Variables in a Single Request. There is, however, a small catch while handling multiple @PathVariable parameters when the path variable string contains a dot(.) character.

How do I apply multiple path variables in Spring boot?

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. }

What does @PathVariable do in Spring MVC?

@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map<String, String> then the map is populated with all path variable names and values. It has the following optional elements: name - name of the path variable to bind to.

Can we use both PathVariable and RequestParam?

@RequestParam and @PathVariable can both be used to extract values from the request URI, but they are a bit different.


1 Answers

@RequestMapping(value = "/{app}/conf/{fnm}", method=RequestMethod.GET)
public ResponseEntity<?> getConf(@PathVariable("app") String app, @PathVariable("fnm") String fnm) {
   log.debug("AppName:" + app);
   log.debug("fName:" + fnm);
           ...
           return ...
  }

Basically path variables need to be specified with parentheses, in method arguments. Does this help?

like image 110
aces. Avatar answered Oct 03 '22 08:10

aces.