5

What does the :.+ at {param:.+} mean in this set of code in java? I have tried searching however i do not find any explanation. Someone who knows please do explain it to me. Thank you so much.

BatchFileController.java

@RequestMapping("/runbatchfileparam/{param:.+}")  
public ResultFormat runbatchFile(@PathVariable("param") String fileName) 
{  
RunBatchFile rbf = new RunBatchFile();  
return rbf.runBatch(fileName);  
}  
Susha Naidu
  • 307
  • 1
  • 3
  • 16

2 Answers2

10

The colon : is separator between the variable name and a regular expression.

The expression .+ means at least one of any character.

Kraylog
  • 7,383
  • 1
  • 24
  • 35
  • Hi, please can you add some trivial example of the meaning? :) – xxxvodnikxxx Dec 15 '17 at 08:29
  • Hi thank you for answering, I would like to give somemore context so that you could help me to better understand this. The URL i have to call is `http://localhost:8080/runbatchfileparam/test123.bat`. If i just put `@RequestMapping("/runbatchfileparam/{param}` i am not able to run the test123.bat file. I have to type it as `@RequestMapping("/runbatchfileparam/{param:.+}` why is that so? – Susha Naidu Dec 15 '17 at 08:30
  • @SushaNaidu that is because of the `.` in the filename, i'm guessing the server assumes that you want to access a file. But with the regex the server knows that the `.` character is allowed too – Lino Dec 15 '17 at 08:33
  • 3
    Spring truncates the file extension in a normal path variable, and doesn't in a regular expression path variable. There's more information [here](https://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated) – Kraylog Dec 15 '17 at 08:37
1

This is used if in case your path variable has . in them. For example, if you want to pass a inner field in mongo as a path variable to fetch from database. (student.address.id). By default everathing after the first dot is ignored. To tell spring framework not to truncate :.+ is used.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134