gpt4 book ai didi

java - Spring Boot 2 index.html 未从映射为静态资源的子目录自动加载

转载 作者:太空狗 更新时间:2023-10-29 18:23:10 25 4
gpt4 key购买 nike

我有一个包含我的 Angular 6 应用程序的 Maven 模块,在构建时它被打包在 META-INF/resources/admin/ui 的 jar 中。

我的 Spring Boot 2 应用程序依赖于前端 Maven 模块,并且在构建它时也包括前端库。但是,如果我访问 http://localhost:8080/admin/ui/ 它会下载一个空的 ui 文件,但是如果我访问 http://localhost:8080/admin/ui/index.html 然后它会显示 Angular 应用程序。

如果我在 META-INF/resources/ 打包前端应用程序,那么 http://localhost:8080/ 将正确显示 Angular 应用程序,但我想要前端应用程序的上下文从 /admin/ui 开始。 Spring Boot 应用程序没有任何自定义映射,它只是用

注释
@Configuration
@EnableAutoConfiguration
@EnableScheduling
@ComponentScan(basePackageClasses = {...})
@Import({...})

是否有我缺少的配置属性?

感谢您的帮助。

最佳答案

你不需要所有这些注释来让它工作......我建议你删除那些不是你故意添加的......!!

要在与主上下文不同的路径上提供静态页面,这里有一个解决方法..!!

创建另一个如下所示的简单 Controller 类..

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Home {

@RequestMapping(path = "/")
public String getHome(){
return "redirect:/admin/ui/";
// make sure no space between colon (:) and endpoint name (/admin/ui)
}

@RequestMapping(path = "/admin/ui/" )
public String getAdminUi(){
return "/index.html";
// your index.html built by angular should be in resources/static folder
// if it is in resources/static/dist/index.html,
// change the return statement to "/dist/index.html"
}

}

而且,请注意,我已将类标记为 @Controller 而不是 @RestController,因此如果您将其标记为 @RestController 或尝试在任何现有的 @RestController 中做同样的事情,你不会轻易实现它。因此,像上面那样创建另一个类没有什么坏处。

这种方式的好处是,它不会破坏您现有的映射。上下文路径也不会改变,因此无需担心您的其他端点路径。他们都将像以前一样工作。

希望这对您有所帮助!!

关于java - Spring Boot 2 index.html 未从映射为静态资源的子目录自动加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51546229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com