Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载

时间:2018-07-26 19:27:50

标签: java angular spring-boot

我有一个包含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({...})

我是否缺少配置属性?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您并不需要所有这些注释才能使其正常工作...我建议您删除那些不是您故意添加的注释。

要在与主上下文不同的路径上提供静态页面,请采用以下解决方法:。

创建另一个简单的控制器类,如下所示。

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或尝试在任何现有的{{1} }您不会轻易实现它。因此,创建像上面这样的另一个类也没有害处。

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

希望这对您有帮助!

相关问题