The Head Rush The Head Rush - 9 months ago 48
Java Question

How Can Spring MVC Handle Requests to URLs Generated by the Angular Router?

I have a Spring (v, 4.3.2 & Java 8) application that serves up an Angular2 single page application front end that has multiple routes (say

, one of which requires a parameter to render (
where x is the identifier of the resource to retrieve).

The html is served up with a standard Spring MVC

@RequestMapping(value = {"/", "/foo", "/bar", "/baz/{id}"}, method = RequestMethod.GET)
public String index(@PathVariable Optional<String> id) {
return "index";

and views are resolved using:

public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
return viewResolver;

If the browser reloads from the
routes, the controller properly returns the
page and angular resolves the view for the route as expected. However, if the browser reloads from one of the
routes that contain a paramenter, the server prepends
to the resource path (e.g.
) which, results in a 404 error.

I've tried refactoring the
request mappings info into an annotation on a distinct (identical) method and using wildcards in the request mapping (e.g.
), but always get the same result. Regardless of the request mapping, Spring seems unaware that it is appending
into the path, as the logs contain:

DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'index'; URL [WEB-INF/pages/index.jsp]] in DispatcherServlet with name 'dispatcher'
DEBUG: org.springframework.web.servlet.view.InternalResourceView - Forwarding to resource [WEB-INF/pages/index.jsp] in InternalResourceView 'index'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request

Any help on this would be appreciated.

Answer Source

Solved the problem - omitting the leading slash from the prefix argument string causes the issue. For proper configuration the argument should be: