I've done a lot of research about this subject and haven't found a correct or specific answer, so I'm asking it here before opening a jira ticket on the Spring MVC project.
My application is built with Spring MVC (with spring boot) for the backend and with AngularJS 1.x on front end. I also have html5mode activated (so, no # used, just plain url like http://podcast.dk.lan/podcasts/123/items/456/player).
I have one @Controller route in the back-end redirecting the "/" request to my index.html. All the others routes are handled by @RestController and works with JSON (on "/api" or "/system").
I've searched for the right way to map all the AngularJS routes to the index.html of my application without breaking the other parts of the back-end resolvers (resource for example).
I've tried the following elements :
@Controller
public class HomeController {
@RequestMapping(value = "/{.*}")
private String home() {
return "forward:/";
}
}
It's not working because, if I try to access directly to a nested url on the front-end (like /podcasts/1/items/1 ), the url is not caught by the request mapping regex.
@Controller
public class HomeController {
@RequestMapping(value = "/**/{.*}")
private String home() {
return "forward:/";
}
}
This configuration lead to a StackOverFlow error because the url will redirect to itself...
Recently, in a really good tutorial about Spring-Security and AngularJS, they used the following pattern:
@Controller
public class HomeController {
@RequestMapping(value = "/{[path:[^\\.]*}")
private String home() {
return "forward:/";
}
}
This pattern excludes all resources (css and js) by excluding any url with a . (dot) in the url. Unfortunately it's not working with nested routes... returning a 404 error.
So, my app backend is now linked to my route front-end, because I have to hard-code the AngularJS route use in the front-end to the request mapping value :
@Controller
public class HomeController {
@RequestMapping(value = {"", "items", "podcasts", "podcasts/**", "player", "podcast-creation", "download", "stats"})
private String home() {
return "forward:/";
}
}
I think (and I hope) there is a better solution to redirect all "not already mapped url backend mapping" to a specific method like this.
All the code of my project is hosted on github (if you want to see more code) at davinkevin/Podcast-Server
Thanks for your help
/toindex.html, and/**for the forward. View controllers will be processed after all your other controllers (lowest precedence).addViewControllersand create 2 view controllers instead of trying to add your own. Mapping the/before/**should work or just create a/**that redirects everything to the index.html file.