Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Spring引导Rest服务和Angular应用程序捆绑到单个war文件中_Angular_Spring Boot_Tomcat - Fatal编程技术网

如何将Spring引导Rest服务和Angular应用程序捆绑到单个war文件中

如何将Spring引导Rest服务和Angular应用程序捆绑到单个war文件中,angular,spring-boot,tomcat,Angular,Spring Boot,Tomcat,遇到这样一种情况:我需要部署一个war文件,其中包含一个角度应用程序,该应用程序在同一个war文件中使用RESTAPI。当与同一服务器中的RESTAPI通信时,我会收到CORS错误 ex Angular app on:localhost:8080/UIApp和REST服务on:localhost:8080/RESTService,正确使用内部服务URL(localhost:8080/RESTService) 部署后,如何正确配置angular应用程序,使用war文件提取的同一文件夹中的服务。这里

遇到这样一种情况:我需要部署一个war文件,其中包含一个角度应用程序,该应用程序在同一个war文件中使用RESTAPI。当与同一服务器中的RESTAPI通信时,我会收到CORS错误

ex Angular app on:localhost:8080/UIApp和REST服务on:localhost:8080/RESTService,正确使用内部服务URL(localhost:8080/RESTService)


部署后,如何正确配置angular应用程序,使用war文件提取的同一文件夹中的服务。

这里有几点需要注意。即使你捆绑了{APP}\Web-INF\Case\static文件夹中的角应用程序——仅仅是-PROD选项,但会有CORS问题,因为角应用程序将考虑服务器内的基本目录。因此,您需要使用CLI使用以下命令构建应用程序:

ng build--base href=/{您的应用程序}/ 这将确保dist文件夹中的文件与{your app}文件夹(HTTP://localhost:8080/{your app})中的文件相同。请注意,在服务器中提取war文件夹时,您需要使用相同的名称

现在,我们最终需要向Spring Boot应用程序的pom文件添加一个插件,以便它将dist文件夹内容复制到\WEB-INF\classes\static文件夹

添加以下插件:

  <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-resources</id>
          <phase>validate</phase>
          <goals><goal>copy-resources</goal></goals>
          <configuration>
            <outputDirectory>${basedir}/target/classes/static/</outputDirectory >
            <resources>
              <resource>
                <directory>${basedir}/../toAngularAppDir</directory >
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
 @Configuration
 public class MvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
        .addResourceLocations("classpath:/static/")
        .resourceChain(true)
        .addResolver(new PathResourceResolver() {
            @Override
            protected Resource getResource(String resourcePath, Resource location) throws IOException {
                Resource requestedResource = location.createRelative(resourcePath);

                return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
                        : new ClassPathResource("/static/index.html");
            }
        });
    }
}