使用单页 angular2 重定向的 Spring Boot

新手上路,请多包涵

我有一个带有 Spring Boot 的单页 Angular 应用程序。它看起来像下面这样:

 src
  main
  java
    controller
       HomeController
       CustomerController
       OtherController
  webapp
    js/angular-files.js
    index.html

Spring Boot 正确默认为 webapp 文件夹并提供 index.html 文件。

我想要做的是:

  1. 对于每个 /api 开头的本地 REST 请求,覆盖并重定向到默认的 webapp/index.html。我计划为弹簧控制器提供任何东西 /api

  2. 有没有办法为所有控制器加上 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/ 这样的 URL some-urlhttp:///localhost/some-other/123/url 然后它将提供 index.html 页面并保留 URL。

在此处输入图像描述

替代方法:尝试添加#ErrorViewResolver: Springboot/Angular2 - How to handle HTML5 urls?

原文由 Robbo_UK 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 412
2 个回答

对于每个不以 /api 开头的本地 REST 请求,覆盖并重定向到默认的 webapp/index.html。我计划为 spring 控制器提供任何 /api。

更新 15/05/2017

让我为其他读者重新表述您的查询。 ( _如有误解请指正_)

背景

使用 Spring Boot 和从类路径提供静态资源

要求

所有 404 非 api 请求都应重定向到 index.html

NON API - 表示 URL 不以 /api 开头的请求。

API - 404 应该像往常一样抛出 404

示例响应

/api/something 将抛出 404

/index.html 将服务器index.html

/something - 将重定向到 index.html

我的解决方案

如果任何处理程序对给定资源不可用,让 Spring MVC 抛出异常。

将以下内容添加到 application.properties

 spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

添加一个 ControllerAdvice 如下

@ControllerAdvice
public class RedirectOnResourceNotFoundException {

    @ExceptionHandler(value = NoHandlerFoundException.class)
    public Object handleStaticResourceNotFound(final NoHandlerFoundException ex, HttpServletRequest req, RedirectAttributes redirectAttributes) {
        if (req.getRequestURI().startsWith("/api"))
            return this.getApiResourceNotFoundBody(ex, req);
        else {
            redirectAttributes.addFlashAttribute("errorMessage", "My Custom error message");
            return "redirect:/index.html";
        }
    }

    private ResponseEntity<String> getApiResourceNotFoundBody(NoHandlerFoundException ex, HttpServletRequest req) {
        return new ResponseEntity<>("Not Found !!", HttpStatus.NOT_FOUND);
    }
}

您可以根据需要自定义错误消息。

有没有办法为所有控制器加上 api 前缀,这样我就不必每次都写 api 了。

为此,您可以创建一个 BaseController 并将RequestMapping路径设置为 /api

例子

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api")
public abstract class BaseController {}

并扩展此 BaseController 并确保您 使用 @RequestMapping 注释子类

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FirstTestController extends BaseController {
    @RequestMapping(path = "/something")
    public String sayHello() {
        return "Hello World !!";
    }

}

上一个答案

您可以创建一个 Filter 重定向到 /index.html 如果请求路径不以 /api

 // CODE REMOVED. Check Edit History If you want.

原文由 ansh 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您厌倦了通过遵循这么多相互冲突的解决方案来解决这个问题 - 请看这里!!

经过几个 小时 尝试遵循来自数十个堆栈溢出和博客文章的所有零散建议,我终于找到了最小的 PURE spring boot + angular 6 应用程序,在非根页面上刷新后总是重定向到 index.html同时维护您所有的 REST API 端点路径。 No @EnableWebMvc , no @ControllerAdvice , no changes to application.properties , no custom ResourceHandlerRegistry modifications, just simplicity:

非常重要的先决条件

*必须*ng build 的输出 包含到 Spring 的 resources/static 文件夹中。您可以通过 maven-resources-plugin 完成此操作。在这里学习: 使用maven将多个资源目录复制到独立的目标目录

代码

@Controller
@SpringBootApplication
public class MyApp implements ErrorController {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String error() {
        return "forward:/index.html";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

推理

  • 在构建时将 ng-build 的输出包含到 resources/static 允许 spring view redirects ( "forward:/index.html" ) 成功。似乎 spring 无法重定向到资源文件夹之外的任何内容,因此如果您尝试访问站点根目录下的页面,它将无法工作。
  • 使用默认功能(即不添加 @EnableWebMvc 或更改 application.properties )导航到 / 自动包含在 index.iff- resources/static 文件夹)所以不需要在那里进行更改。
  • 使用默认功能(如上所述),在 spring boot 应用程序中遇到的任何错误都会路由到 /error 并实现 ErrorController 覆盖该行为 - 你猜对了 - 路由到 index.html 允许 Angular 接管路由。

评论

  • 不要满足于 HashLocationStrategy 来解决这个问题,因为 Angular 不推荐它: https ://angular.io/guide/router#which-strategy-is-best

原文由 Reuben Tanner 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题