簡體   English   中英

帶有Spring Boot的Angular CLI

[英]Angular CLI with Spring Boot

我有2個項目。 我使用Angular-cli構建的Angular2應用程序和僅用於Angular2應用程序的Spring Boot應用程序。 我用ng build來構建Angular2應用,這會生成一個dist文件夾。 然后,我將dist文件夾的內容放在src/main/resources/static中的Spring Boot應用程序中。

我的Spring Boot應用程序有兩個文件。

Spring啟動應用程序類:

@SpringBootApplication
public class SpringBoot extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBoot.class);
    }

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

以及application.properties文件:

server.contextPath=/
server.port=80

它運作良好,但是如果我進入URL並單擊刷新按鈕, Whitelabel Error Page 我知道這是因為URL與資源文件不匹配時,URL不提供index.html

如果網址與資源文件不匹配,如何配置Spring Boot應用程序以提供index.html

您是正確的,需要為Spring未知的端點提供index.html 然后安排Angular應用程序管理未知路線。

我使用WebMvcConfigurerAdapter處理這種情況。 還要在此處放置靜態內容文件類型。

添加一個config目錄,並在其中添加具有以下內容的Java文件WebMvcConfig (例如):

package com.yourdomain.yourapp.config;

import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.PathResourceResolver;

import java.io.IOException;
import javax.inject.Inject;

@Configuration
@EnableConfigurationProperties({ ResourceProperties.class })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

  @Inject
  private ResourceProperties resourceProperties = new ResourceProperties();

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    Integer cachePeriod = resourceProperties.getCachePeriod();

    final String[] staticLocations = resourceProperties.getStaticLocations();
    final String[] indexLocations = new String[staticLocations.length];
    for (int i = 0; i < staticLocations.length; i++) {
      indexLocations[i] = staticLocations[i] + "index.html";
    }
    registry.addResourceHandler(
      "/**/*.css",
      "/**/*.html",
      "/**/*.js",
      "/**/*.json",
      "/**/*.bmp",
      "/**/*.jpeg",
      "/**/*.jpg",
      "/**/*.gif",
      "/**/*.ico",
      "/**/*.png",
      "/**/*.ttf",
      "/**/*.wav",
      "/**/*.mp3",
      "/**/*.eot",
      "/**/*.svg",
      "/**/*.woff",
      "/**/*.woff2",
      "/**/*.map"
    )
      .addResourceLocations(staticLocations)
      .setCachePeriod(cachePeriod);

    registry.addResourceHandler("/**")
      .addResourceLocations(indexLocations)
      .setCachePeriod(cachePeriod)
      .resourceChain(true)
      .addResolver(new PathResourceResolver() {
        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
          return location.exists() && location.isReadable() ? location : null;
        }
      });
  }
}

我認為您還必須為組件掃描指定配置包。 也許沒有先嘗試,看看是否可行。

@SpringBootApplication
@ComponentScan( basePackages = { "com.yourdomain.yourapp.config" })
public class SpringBoot extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringBoot.class);
  }

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

如果您缺少依賴項。 這就是我的build.gradle

dependencies {
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
  compile group: 'javax.inject', name: 'javax.inject', version: '1'

  optional group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'

  providedRuntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat'

  testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

希望這可以幫助 :-)

在這里,您可以找到我的Spring Boot 2和Angular 6入門項目。

Spring Boot應用程序是使用SPRING INITIALIZR創建的,Angular應用程序是使用Angular CLI創建的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM