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.
Another way to define an optional path variable that is available since Spring 3.2 is with a Map for @PathVariable parameters: @RequestMapping(value = {"/article", "/article/{id}"}) public Article getArticle(@PathVariable Map<String, String> pathVarsMap) { String articleId = pathVarsMap. get("id"); if (articleId !=
Optional Path variables in spring boot can be configured using the annotation @PathVariable. if the path variable is not configured as optional and the value of the path variable is not available in the request url, an error will be thrown.
Usecase when need optional @PathVariable E.g. we may have a method in Spring MVC controller which can be used to create a new record or edit an existing record – based on the presence of 'id' attribute of record.
Using @PathVariable required attribute From Spring 4.3. 3 version, @PathVariable annotation has required attribute, to specify it is mandatorily required in URI. The default value for this attribute is true if we make this attribute value to false, then Spring MVC will not throw an exception.
Spring MVC – Optional @PathVariable in request URI By design, in Spring MVC, it is not possible to have optional @PathVariable value. Still, we can use multiple path mappings to simulate this effect successfully.
Binding Request Path Variable to Spring Handler Methods: Spring MVC provides @PathVariable to bind all request path variable in the handler methods. It uses the @RequestMapping URI template to bind all path variables.
If you are using Spring 4.1 and Java 8 you can use java.util.Optional which is supported in @RequestParam, @PathVariable, @RequestHeader and @MatrixVariable in Spring MVC - Show activity on this post. It's not well known that you can also inject a Map of the path variables using the @PathVariable annotation.
How Spring Binds @PathVariable Parameters By default, Spring will try to bind all parameters annotated with @PathVariable in a handler method with the corresponding variables in the URI template. If Spring fails, it'll not deliver our request to that handler method.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With