Announcement Announcement Module
Collapse
No announcement yet.
@RequestParam + @PathVarible Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @RequestParam + @PathVarible

    Hello everyone,

    I'm having this issue that I can't seem to solve, let me show you. I have this method on my controller:

    Code:
    @RequestMapping(value = "/inventory/pepe/{firstName}/{lastName}", method = RequestMethod.GET)
        public String pepe(@PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName) {
    
        	logger.info("First name: " + firstName + " Last name: " + lastName);
        	return "inventory/pepe";
        	
        }
    Which works great if the URI is formed complete (http://app/inventory/pepe/firstName/lastName). So if I go to http://app/inventory/pepe or http://app/inventory/pepe/firstName I just get an error (or if I have a method with a RequestMethod.GET it goes to the first one by default.

    Is there a way to use the same RequestMapping for multiple possibilities like:

    /inventory/pepe/{firstName}/{lastName}
    /inventory/pepe/{firstName}
    /inventory/pepe

    And then add some logic to check or return whatever is needed?

    Thank you.

  • #2
    Thanks to the spring guys!!!

    Thanks to help of the spring guys I was able to fix this issue.

    Is pretty simple actually when you look at it. The only way to try to map something like that and knowing that PathVariable is required (the value per se) you need to have different mappings for the conbinations you need like this:

    Code:
    @RequestMapping(value = {"/inventory/pepe/{firstName}/{lastName}"}, method = RequestMethod.GET)
        public String pepe(@PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName) {
    
        	logger.info("First name: " + firstName + " Last name: " + lastName);
        	return "inventory/pepe";
        	
        }
        
        @RequestMapping(value = {"/inventory/pepe/{firstName}"}, method = RequestMethod.GET)
        public String pepe(@PathVariable("firstName") String firstName) {
        	
        	return this.pepe(firstName, "");
        	
        }
    As you can see I have a request /inventory/pepe/{firstName} which maps all the /inventory/pepe/1 or /inventory/pepe/whatever and calls the main method pepe with the double parameters. So actually you can always call the methods with the longer number of parameters (for reuse) and send default values.

    If would be very weird to accept in PathVariable default values because the Mapping wouldn't make any sense at all they would be mappings like

    "/inventory/pepe/{firstName}/list" would map to /inventory/pepe/list which is not right for the mapping since you are expecting actually a real path like "/inventory/pepe/david/list".

    I hope this helps.

    Thanks again to Mr. Poutsma for his help.

    Comment

    Working...
    X