cocovan
cocovan

Reputation: 51

Exclude Angluar2 route from Spring Web MVC application

I have a spring-boot application that has a few views set up. I also have bundled an Angular2 app. When I load the Angular2 app, all works fine, however, when I try to deep link to a route within the application, Spring MVC is intercepting the call, failing to find an associated view and returning the error page.

http://localhost:8080/index.html will load the Angular2 application which then re-writes the URL to be just http://localhost:8080/. If I then navigate to the route I want e.g. http://localhost:8080/invite/12345, then the route loads and works as expected. Hitting http://localhost:8080/invite/12345 directly returns the standard Spring MVC error page.

However, if I run the Angular2 app as a standalone application (not served up by spring-boot), then hitting that link directly works as expected. it loads the index.html, fires the route and shows me the data I want.

How can I, via Java configuration, tell Spring to ignore the /invite/** path (and other paths too as my Angular2 app grows) so I can deep-link to routes within my Angular2 application. Here's the current Java configuration:

@SpringBootApplication
@EnableResourceServer
public class AuthserverApplication extends WebMvcAutoConfigurationAdapter {

    public static void main(String[] args) {
        SpringApplication.run(AuthserverApplication.class, args);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/oauth/confirm_access").setViewName("authorize");
        registry.addViewController("/success").setViewName("success");
    }

    @Bean
    public ViewResolver getViewResolver() {
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".html");
        return resolver;
    }
}

This project is inheriting from spring-boot 1.3.3.RELEASE:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

Upvotes: 3

Views: 1021

Answers (2)

Huka
Huka

Reputation: 390

Based on your answer, I config like this and it's worked.

@RequestMapping(value = "/*", method = RequestMethod.GET)
@Override
public String index()
{
    return "index";
}

@Override
@RequestMapping(value = "/login/*", method = RequestMethod.GET)
public String login()
{
    return "redirect:/#/login";
}

So we can access to localhost:8080/angular-app/login/ without 404 error.

Upvotes: 1

cocovan
cocovan

Reputation: 51

So the way I got round this in the end was to link directly to the index.html page so that it forced the angular2 app to load e.g.:

http://localhost:8080/index.html/#/invite/12345

Upvotes: 0

Related Questions