【发布时间】:2023-03-13 02:05:01
【问题描述】:
我有一个带有 Spring Boot 的单页 Angular 应用程序。如下所示:
src
main
java
controller
HomeController
CustomerController
OtherController
webapp
js/angular-files.js
index.html
Spring boot 正确默认为 webapp 文件夹并提供 index.html 文件。
我想做的是:
对于每个以
/api开头的本地 REST 请求不,覆盖并重定向到默认 webapp/index.html。我计划为弹簧控制器提供任何/api服务。-
有没有办法为所有控制器添加 API 前缀,这样我就不必每次都编写 API? 例如
@RequestMapping("/api/home") 可以在代码中写简写@RequestMapping("/home")
或
@RequestMapping("/api/other-controller/:id") can write shorthand @RequestMapping("/other-controller/:id")
我正在寻找每个 API 请求,例如1) http://localhost:8080/api/home 保留 API 和 API 并解析正确的控制器并返回 JSON,但是如果有人输入像 http:///localhost/some-url 或 http:///localhost/some-other/123/url 这样的 URL,那么它将提供 index.html 页面并保留 URL。
其他方法:尝试添加#ErrorViewResolver: Springboot/Angular2 - How to handle HTML5 urls?
【问题讨论】:
-
您可以尝试创建一个自定义注释,其中将包含
@RequestMapping("/api")并将其应用于您的 api 控制器。然后在特定网址的方法上使用@RequestMapping。 -
这个问题可能还有一个额外的要求:index.html 可能会引用 js 和 css 文件,虽然不是“index.html”,但不应作为 /api/* 处理* 请求
标签: java spring angular spring-mvc spring-boot