我的Spring Boot + Angular 6 + Gradle应用程序出现问题。
我的应用程序如下所示:
父母:
->后端
->前端
后端使用Spring Boot并配置如下:
@SpringBootApplication
@EnableBatchProcessing
public class BackendApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApplication(builder);
}
public static void main(String[] args) {
configureApplication(new SpringApplicationBuilder()).run(args);
}
private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
return builder.sources(BackendApplication.class).bannerMode(Banner.Mode.OFF);
}
}
@Controller
@EnableWebMvc
public class IndexController extends WebMvcConfigurerAdapter {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest request, Model model) {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("apiUrl", resolveBaseApiUrl(request));;
return modelAndView;
}
private String resolveBaseApiUrl(HttpServletRequest request) {
return request.getRequestURL().toString().replace("https://", "//").replace("http://", "//");
}
}
@EnableWebMvc()
@Configuration
public class ViewConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/classes/");
bean.setSuffix(".jsp");
return bean;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/WEB-INF/classes/");
}
}
此外,我还应该添加一些依赖项才能从战争中运行该应用程序。
compile 'javax.servlet:jstl:1.2'
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.10'
我正在构建我的Angular 6前端,并使用gradle sourceSets将dist目录的内容添加到WAR:
sourceSets {
main {
resources {
srcDirs = ["$webappDir/dist", "$projectDir/src/main/resources"]
}
}
}
构建后的WAR文件如下所示(如您所见,有WEB-INF / classes / index.jsp文件): Screenshot of WAR file
您有什么想法为什么我仍然要404?
Whitelabel Error Page
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/classes/index.jsp
答案 0 :(得分:0)
我自己也遇到类似的问题。我认为 Spring Boot正在static
内寻找classes
目录。
有关详情,请参见this link。对于Spring Boot应用程序来说,它似乎可以将您的静态内容(包括index.html)放在以下任何位置:
spring.io链接的评论部分中有很多讨论有关类似挑战的人。我正在努力通过它来尝试正确设置我的设置。 https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
您的sourceSets似乎会将Angular内容放到main/resources
中,但是生成的WAR屏幕截图中没有/resources
目录。也许尝试将sourceSets更改为:
sourceSets {
main {
resources {
srcDirs = ["$webappDir/dist", "$projectDir/src/main/resources/static"]
}
}
}
我认为这将创建静态目录,然后当您按下localhost:8080/WARname
答案 1 :(得分:0)
问题是您配置的资源请求路径应为 在
/WEB-INF/classes/
中搜索,这就是为什么在此路径/WEB-INF/classes/index.jsp
上出现错误的原因
现在,如果您使用的是AngularJS并且它是一个单页应用程序,则配置应类似于:
@EnableWebMvc()
@Configuration
public class ViewConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/static/js/**")
.addResourceLocations("/resources/static/js/");
registry.addResourceHandler("/resources/static/css/**")
.addResourceLocations("/resources/static/css/");
registry.addResourceHandler("/resources/static/views/**")
.addResourceLocations("/resources/static/views/");
registry.addResourceHandler("/resources/static/**")
.addResourceLocations("/resources/static/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/");
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/views/index.html")
.setCachePeriod(cachePeriod).resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath,
Resource location) throws IOException {
return location.exists() && location.isReadable() ? location :
null;
}
});
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/resources/static/views/");
resolver.setSuffix(".html");
return resolver;
}
}
这种方式是说我的js,css,image之类的资源应在其对应的文件夹中进行搜索,而所有其他路径应由index.html在classpath:/static/views/index.html
- 请注意,您可能应该更改上述代码以匹配您的文件夹结构。我希望你能理解整个想法。