首页 > 解决方案 > Angular 项目不适用于 Spring Boot

问题描述

我想要做的是将前端应用程序与 Spring Boot 后端一起运行。

使用ng serve.

我尝试使用ng build --prod然后将文件从dist文件夹复制到src/main/resources/static. 之后我用来clean install生成 jar 文件并使用java -jar %filename.jar%.

Spring Boot 应用程序似乎正在正确启动,REST 端点可用,但是,当我尝试在 localhost:8080 下获取前端视图时,我在终端中获得了Whitelabel 错误页面No mapping for GET /信息。

假设是否正确,例如,如果 localhost:4200/products 显示带有某些产品的表(ng serve方法),那么 localhost:8080/products(在 jar 方法中)应该显示同一个表?还是我错过了一些步骤来实现它?

我尝试使用哈希策略,但它似乎没有改变任何东西。

应用程序路由.module.ts

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'sign-up', component: SignUpComponent },
  { path: 'login', component: LoginComponent },
  { path: 'products', component: ProductsComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: true })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

索引.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My Application</title>
  <base href="#">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.cd71f88743b54f1de66b.css"></head>
<body>
  <app-root></app-root>
<script src="runtime.acf0dec4155e77772545.js" defer></script><script src="polyfills.3ea10c1644fbb8005130.js" defer></script><script src="scripts.a73844de5f291be94c3b.js" defer></script><script src="main.abbc3215607e4963f8f1.js" defer></script></body>
</html>

标签: angularspring-boot

解决方案


“/”是您要调用的默认 GET URL 吗?我想您可以通过直接在浏览器中粘贴他们的 GET URL 来访问其他端点吗?如果是这种情况,您能否检查浏览器网络日志中调用的 URL 是什么?如果这一切看起来都很好,那可能是 Angular 路由的问题。如果您使用的是 HashLocationStrategy,请尝试在 index.html 文件的头部添加。


推荐阅读