6

Reading this (Spring 3) article from 2010, it discusses using an extension to provide a convenient way to include optional path segments:

@RequestMapping("/houses/[preview/][small/]{id}")
public String handlePreview(@PathVariable long id, @PathVariable("preview/") boolean preview, @PathVariable("small/") boolean small) {
    return "view";
}

I know I could just implement a number of request mappings to achieve the same effect:

@RequestMapping(value="/houses/preview/{id}")
...

@RequestMapping(value="/houses/{id}")
...
~~~ snip ~~~

But depending on the number of possible permutations, it seems like a very verbose option.

Does any later version of Spring (after 3) provide such a facility? Alternatively, is there any mechanism to chain portions of the request URL to feed a larger response method signature?

Update

This answer to a question relating to sharing path variables and request parameters suggests an approach like:

@RequestMapping(method=RequestMethod.GET, value={"/campaigns","/campaigns/{id}"})
    @ResponseBody
    public String getCampaignDetails(
         @PathVariable("id") String id)
    {
        ~~~ snip ~~~

But the path variable cannot be set to null. Just going to /campaigns will return a 400 response.

Michael
  • 7,348
  • 10
  • 49
  • 86
  • 1
    Do you want like http://stackoverflow.com/questions/2745471/spring-web-mvc-use-same-request-mapping-for-request-parameter-and-path-variable#2746361 – user3145373 ツ Sep 11 '14 at 09:49
  • Nearly, Javi's answer with the multiple request values - `value={"/campaigns","/campaigns/{id}"}` is very nearly there, but I'd like to map fixed segments in the URL. – Michael Sep 11 '14 at 10:05
  • this question already discussed on one forum, this is same asked here: http://java.dzone.com/articles/spring-3-webmvc-optional-path – user3145373 ツ Sep 11 '14 at 10:07
  • Yes, that's the article I link to in my question... hence why I'm asking for alternatives. ;) – Michael Sep 11 '14 at 10:11
  • 1
    If all else fails, you could always rewrite the URLs with filter like this one: http://tuckey.org/urlrewrite/ – Krešimir Nesek Sep 11 '14 at 13:07
  • What about `/houses/**/{id}`? – a better oliver Sep 11 '14 at 19:04
  • Possible duplicate of [With Spring 3.0, can I make an optional path variable?](https://stackoverflow.com/questions/4904092/with-spring-3-0-can-i-make-an-optional-path-variable) – Oleg Estekhin Aug 03 '17 at 16:39
  • @OlegEstekhin The link and body of my question already acknowledged the approach in the question you've linked to. My question is asking for alternatives in later versions of spring (have edited to make that clearer). – Michael Aug 04 '17 at 10:44

1 Answers1

8

Why you don't use java.util.Optional if you are using Java 1.8. To take your later example, you can avert a 400 response with put a Optional<String> instead of String representing your eventualy path like this:

@RequestMapping(method=RequestMethod.GET, value={"/campaigns","/campaigns/{id}"})
@ResponseBody
public String getCampaignDetails(
     @PathVariable("id") Optional<String> id)
{ 
  if(id.isPresent()){
      model.addAttribute("id", id.get());//id.get() return your String path
  }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Tanorix
  • 230
  • 2
  • 16