Angular 和Spring休息不良路径

ymdaylpp  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(173)

当我输入unexist/bad id of movie get(例如/movies/asd)时,我只看到没有后端属性的html(清除frontend而没有值)。spring(在控制台中)的错误是404,它很好,后端工作正常,但是如何处理这个异常,不显示前端,只需重定向到home或show404站点。
spring(在这种情况下,服务抛出illegalargumentexception):

@GetMapping
    ResponseEntity<List<MovieReadModel>> getAllMovies() {
        logger.warn("Exposing all the movies!");
        return ResponseEntity.ok(service.readAllMovies());

    @ExceptionHandler(IllegalArgumentException.class)
    ResponseEntity<String> handleIllegalArgument(IllegalArgumentException e) {
        return ResponseEntity.notFound().build();
    }

    @ExceptionHandler(IllegalStateException.class)
    ResponseEntity<String> handleIllegalState(IllegalStateException e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }
}

Angular :

export class MovieDetailsComponent implements OnInit {
  currentMovie: Movie = {
    title: '',
    description: '',
    director: '',
    producer: '',
    category: '',
    actors: ['']
  };

  constructor(
    private movieService: MovieService,
    private route: ActivatedRoute
    ) { }

  ngOnInit(): void {
    this.getMovie(this.route.snapshot.params.id)
  }

  getMovie(id: string): void {
    this.movieService.get(id)
      .subscribe(
        data => {
          this.currentMovie = data;
          console.log(data);
        },
        error => {
          console.log(error);
        });
  }

}

路由:

const routes: Routes = [
  { path: 'movies', component: MoviesListComponent},
  { path: 'movies/:id', component: MovieDetailsComponent},
  { path: 'add', component: AddMovieComponent},
  { path: 'profile', component: ProfileComponent },
  { path: 'register', component: RegisterComponent },
  { path: 'login', component: LoginComponent },
  { path: 'user', component: BoardUserComponent },
  { path: 'admin', component: BoardAdminComponent },
  { path: '', redirectTo: 'movies', pathMatch: 'full' }
];

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题