我有2个Spring Boot应用程序,一个充当数据提供者,一个只是我的Angular 6应用程序的包装,以便能够使用java -jar JAR_NAME
将其部署在服务器上。
在使用angular-cli处理我的Angular应用并使用ng serve
运行它时,我可以在http://localhost:4200
看到第一页,该页面将我重定向到localhost:4200 / index。我有另一个映射到/ comp的组件,并且在访问localhost:4200 / comp时在页面上显示了新组件。
当我将应用程序包装在spring jar中并尝试直接从Angular访问路由时,会出现问题。我的spring应用程序在localhost:8999上提供,并在命中后将我重定向到localhost:8999 / index,并且页面加载正常。如果单击将我重定向到comp
页的按钮,则该URL更改为localhost:8999 / comp,并且资源得到正确提供。问题是,如果我访问localhost:8999 / index或localhost:8999 / comp,则会收到404错误。
相关代码段:
From application.yml:
zuul:
ignoredServices: '*'
routes:
g-api:
path: /loc/**
url: http://localhost:8443/api
来自app-routing.module.ts:
const routes: Routes = [
{path: '', redirectTo: '/index', pathMatch: 'full'},
{path: 'index', component: FirstComp},
{path: 'comp', component: SecondComp}}
来自导航栏html:
<li class="n">
<a class="na"
[routerLink]="['/comp']" >
<i class="a"></i>
<p>comp</p>
</a>
</li>
请注意,即使与spring一起部署,zuul代理也可以正常工作,因此我能够从部署在不同端口上的API检索数据。但是,如果我通过URL刷新或访问某些Angular组件,则会得到404。
PS:这里不是前端开发人员。
答案 0 :(得分:0)
我如何解决此问题:
@Configuration
public class WebApplicationConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/notFound").setViewName("forward:/index.html");
}
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
return container -> {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
"/notFound"));
};
}
}
在Spring上,将所有未找到的配置为转到/notFound
,然后添加一个ViewController
来转发到您的index.html
。这是用于弹簧靴2