在Spring Boot和Angular 6应用程序中,可以通过自定义错误处理器来处理白标签错误页面404。下面是一种可能的解决方法:
CustomErrorController
。import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.NoHandlerFoundException;
@RestController
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public ResponseEntity> handleError() {
// 处理404错误
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 Not Found");
}
@Override
public String getErrorPath() {
return "/error";
}
}
index.html
文件中添加一个基本的错误页面,例如:
Page Not Found
404 Page Not Found
The requested resource could not be found.
app.module.ts
文件中添加一个路由规则,将所有未匹配的路径重定向到错误页面。import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
// 添加以下路由规则
{ path: '**', redirectTo: '/404' }
];
@NgModule({
imports: [BrowserModule, RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppModule { }
注意:确保在Spring Boot中配置了静态资源的访问路径,以便加载Angular 6应用程序的构建文件。