angularjs - 如何在同一个域/服务器上部署 AngularJS 应用程序和 Spring Restful API 服务?

标签 angularjs spring tomcat spring-boot yeoman

我是 Spring 和 AngularJS 的新手。我按照步骤 here构建后端restful API,并根据请求发送Json。因此,根据指南,当我运行“mvn spring-boot:run”时,tomcat 服务器从 localhost:8080 开始。

然后我使用 Yeoman Angular 生成器来构建我的 Angular 应用程序。当我在 Angular 应用程序中运行“grunt serve”时,前端应用程序在 localhost:9000 运行。

我应该怎么做才能让我的 Angular 应用程序与我的 Springboot Tomcat 服务器一起在同一个域上提供服务,比如 localhost:8080 ?

是否有我可以遵循的示例项目?我找到了以下项目,但由于我对 tomcat 没有太多背景,因此仍然无法使其工作。

  1. https://github.com/robharrop/spring-angularjs
  2. https://github.com/GermanoGiudici/angularjs-maven-tomcat-seed
  3. https://github.com/xvitcoder/spring-mvc-angularjs

最佳答案

您需要采取以下任一方式:

  • 构建的文件(grunt build 然后基本上是 dist/目录中的所有内容)
  • 原始文件(您的 index.html 和所有 JS 原样)

并将它们复制到以下文件夹之一(我推荐/public/):http://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

Spring Boot will automatically add static web resources located within any of the following directories:

/META-INF/resources/

/resources/

/static/

/public/

This means that not only does Spring Boot offer a simple approach to building Java or Groovy apps, you can also use it to easily deploy client-side JavaScript code and test it within a real web server environment!

这对开发来说会很痛苦,但是因为每次对前端进行更改时都必须重新复制文件。对于生产,您的目标应该是使用您的 spring 应用程序部署构建文件的版本化副本。

对于开发,您可能需要考虑让 grunt 提供 Angular 内容并同时运行 Tomcat 和 grunt 服务器(它是节点吗?)并在前端和后端之间启用跨源请求。或者您可以将整个 angular 目录复制到上述目录之一,但这是一种短期方法。

关于angularjs - 如何在同一个域/服务器上部署 AngularJS 应用程序和 Spring Restful API 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920508/

相关文章:

javascript - 在 Controller 中更改 AngularJS 中的 ng-style 值

javascript - 如何根据 Angular 中的表达式从元素中删除属性?

java - 无法加载在 Docker 中运行的 Spring Boot 应用程序

java - 使用 Junit 测试 Spring Boot 应用程序(需要 Autowiring 构造函数的类的测试方法)

java - spring mvc中如何使用静态资源?

javascript - Tomcat servlet,将所有网址重定向到单个网页

angularjs - 带有 fa 图标的内联 ng-if

angularjs - ngOnInit 和 Constructor 被调用两次

ssl - TLS 协议(protocol)定义的致命警报代码是 70

java - 在 Tomcat 中 session 集群 Grails 应用程序