Search code examples
springspring-mvcjax-rsrequest-mapping

Migrate from JAX-RS @Path to Spring MVC @RequestMapping using regex


I'm trying to migrate code from Jax-Rs (jersey implementation) to a Spring MVC entry point:

jax-rs:

@GET
@Path("{bundle}/bundle{min: (-min)*}.{extension: js|css}")
public Response getBundle(@PathParam("bundle") String bundle, @PathParam("min") String min, @PathParam("extension") String extension)

Spring MVC:

@RequestMapping(method = GET, path = "{bundle}/bundle{min:(-min)?}{extension:\\.(js|css)?}")
public void getBundle(@PathVariable String bundle, @PathVariable String min, @PathVariable String extension)

According to Spring MVC documentation, i can use regex for @RequestMapping. The syntax is similar to jaxrs but the entrypoint doesn't work (404 Not Found).

Example of value for the entrypoint: http://localhost:8080/foo/bundle-min.css

I've found a solution with @RequestMapping(method = GET, path = "{bundle}/bundle**") but i have to parse the string to catch my needed parameter values.


Solution

  • Your regex seems fine but replace those capturing groups with non-capturing groups, like following:

    {bundle}/bundle{min:(?:-min)?}{extension:\.(?:js|css)?}
    

    With this regex, if you fire a request to foo/bundle-min.css, the bundle would be foo, the min would be -min and the extension would be .css.