I'm migrating some routes from a jax-rs based application to SpringBoot. In jax-rs I can use @Path to define a regex that contains multiple URL path elements:
@Path("{id:[^/]+/y[\d]{4}/m[\d]{1,2}/d[\d]{1,2}/h[\d]{1,2}}/")
The id variable in the method body will then be the matching segment of the URL and I can go about my day.
With @RequestMapping in Spring this doesn't work. As soon as you put a forward slash into the regex you get a PatternParseException.
PathContainer pathContainingSlash = PathContainer.parsePath("/api/test/y1978/m07/d15");
PathPatternParser parser = new PathPatternParser();
assertThrows(PatternParseException.class, () ->
parser.parse("/api/test/{ticketId:y[\\d]{4}/m[\\d]{1,2}/d[\\d]{1,2}}"));
This same problem appears to happen with AntPathMatcher.
AntPathMatcher antPathMatcher = new AntPathMatcher();
assertThrows(IllegalStateException.class, () ->
antPathMatcher.extractUriTemplateVariables(
"/api/test/{ticketId:y[\\d]{4}/m[\\d]{1,2}/d[\\d]{1,2}}",
"/api/test/y1978/m07/d15"));
This is a problem because I have about 78 of these URL patterns. I'm going to have to define each pattern individually with each path element being a separate variable. Then I'm going to have to use String concatenation to combine them back together in the format of a path.
@GetMapping("/{year:y[\\d]{4}}/{month:m[\\d]1,2}/{day:d[\\d]{1,2}")
public ResponseEntity<Void> foo(@PathVariable String year,
@PathVariable String month,
@PathVariable String day) {
String date = year + "/" + month + "/" + day;
}
Other than using Jax-rs in my SpringBoot app, is there accomplish this? It's possible to write them all like this but it seems sub-optimal.
For clarity, I really want a way to extract multiple path elements from a URL into a @PathVariable. I would like something like this:
@GetMapping("/api/test/{date:y[\\d]{4}/m[\\d]{1,2}/d[\\d]{1,2}}")
public ResponseEntity<Void> foo(@PathVariable String date) {}
So that date is now equal to y1978/m07/d15
Also, this is just one example pattern. There are 78 unique patterns, that have a varying number of a path elements and contents of the elements. In Jax-RS using @Path I can OR these regexes together and create one route and the path variable is accessible inside the method.