[英]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.