javaangularjsspring

Html5 routing with Spring and Angular


I'm trying to implement HTML5 routing with Spring-boot and Angular 1.5, following this article.

At some point I need to redirect all the angular routes to the base path with a controller like this:

@Controller
 public class UrlController {
  // Match everything without a suffix (so not a static resource)
  @RequestMapping(value = "/{path:[^\\.]*}"))
  public String redirect(HttpServletRequest request) {
    // Forward to home page so that route is preserved.
    return "forward:/";
  }
}

While the regular expression matches most of the URLS, like dashboard, search etc:

 2016-08-05 16:39:58 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:306 - Looking up handler method for path /dashboard
 2016-08-05 16:39:58 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:313 - Returning handler method UrlController.redirect(javax.servlet.http.HttpServletRequest,java.lang.String)]
 2016-08-05 16:39:58 DEBUG o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'urlController'
 2016-08-05 16:39:58 DEBUG o.s.w.s.DispatcherServlet:947 - Last-Modified value for [/dashboard] is: -1
 2016-08-05 16:39:58 DEBUG c.a.a.w.i.LoggingInterceptor:22 - Start processing url request: https://localhost:8443/dashboard
 redirecting to home page from url https://localhost:8443/dashboard
 2016-08-05 16:39:58 DEBUG c.a.a.w.i.LoggingInterceptor:35 - Finished processing url request: https://localhost:8443/dashboard, duration: 0[ms]

Others are ignored and not matched at all, like https://localhost:8443/faults/64539352

 2016-08-05 17:00:05 DEBUG o.s.w.s.DispatcherServlet:861 - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/faults/64539352]
 2016-08-05 17:00:05 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:306 - Looking up handler method for path /faults/64539352
 2016-08-05 17:00:05 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:316 - Did not find handler method for [/faults/64539352]

The regular expression seems to be fine if you test it in regexplanet , but it does not match what I want if I put it in the @RequestMapping. Does anybody know how to make it work, or even better how to do it without regular expressions?


Solution

  • The problem and solution is described here. The solution consist in the implementation of a OncePerRequestFilter where you can match the full URI against whatever you want.