java - 从 Spring Web MVC 应用程序中排除 Angluar2 路由

标签 java spring spring-mvc angular spring-boot

我有一个设置了几个 View 的 spring-boot 应用程序。我还捆绑了一个 Angular2 应用程序。当我加载 Angular2 应用程序时,一切正常,但是,当我尝试深层链接到应用程序内的路由时,Spring MVC 拦截调用,无法找到关联的 View 并返回错误页面。

http://localhost:8080/index.html将加载 Angular2 应用程序,然后将 URL 重写为 http://localhost:8080/ .如果我然后导航到我想要的路线,例如http://localhost:8080/invite/12345 ,然后路线加载并按预期工作。命中 http://localhost:8080/invite/12345直接返回标准的Spring MVC错误页面。

但是,如果我将 Angular2 应用程序作为独立应用程序运行(不由 spring-boot 提供),那么直接点击该链接会按预期工作。它加载 index.html,触发路由并显示我想要的数据。

我如何通过 Java 配置告诉 Spring 忽略 /invite/** 路径(以及随着我的 Angular2 应用程序增长的其他路径),以便我可以深层链接到我的路由Angular2 应用程序。这是当前的 Java 配置:

@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;
    }
}

本项目继承自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>

最佳答案

根据您的回答,我这样配置并且成功了。

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

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

因此我们可以访问 localhost:8080/angular-app/login/而不会出现 404 错误。

关于java - 从 Spring Web MVC 应用程序中排除 Angluar2 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478011/

相关文章:

java - 从 Java 代码执行 Shell 脚本

java - 最终的 java 应用程序的文件类型是什么?

java - Quartz 2.2.3 JobStore 属性被默认设置覆盖 - Spring Boot、Liquibase、Oracle

Spring 启动自动配置。意外的行为

java - Tomcat:无法销毁与 ProtocolHandler 关联的端点

java - 在 Java 中访问静态字段的正确方法是什么?

java - 通过 Stripes 将 POST 数据映射到对象

java - 为什么 spring boot angularjs gateway 应用程序不能从 ui 应用程序读取?

spring-mvc - spring 3.0 中如何绑定(bind)请求参数?

java - 使用响应实体的动态 JSON 对象 - Spring