Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载 [英] Spring Boot 2 index.html not loaded automatically from subdirectory mapped as static resource

查看:86
本文介绍了Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我是否缺少配置属性?

感谢您的帮助.

解决方案

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

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

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

  import org.springframework.stereotype.Controller;导入org.springframework.web.bind.annotation.RequestMapping;@控制器公共课首页{@RequestMapping(路径="/")公共字符串getHome(){返回重定向:/admin/ui/";//确保冒号(:)和端点名称(/admin/ui)之间没有空格}@RequestMapping(path ="/admin/ui/")公共字符串getAdminUi(){返回"/index.html";//由angular创建的index.html应该位于resources/static文件夹中//如果位于resources/static/dist/index.html中,//将return语句更改为"/dist/index.html"}} 

而且,请注意这里,我已将该类标记为 @Controller 而不是 @RestController ,因此,如果将其标记为 @RestController 或尝试在任何现有的 @RestController 中执行相同的操作,您将很难轻松实现.因此,创建像上面这样的另一个类也没有什么害处.

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

希望这对您有帮助!

I have a Maven module containing my Angular 6 application, and at build it is packaged in a jar at META-INF/resources/admin/ui.

My Spring Boot 2 application has a dependency to the frontend Maven module and when building it includes the frontend library as well. However, if I access http://localhost:8080/admin/ui/ it downloads an empty ui file, but if I access http://localhost:8080/admin/ui/index.html then it displays the Angular application.

If I package the frontend application at META-INF/resources/ then http://localhost:8080/ will display the Angular application correctly, but I want the context of the frontend application to start from /admin/ui. The Spring Boot application does not have any custom mappings, it is just annotated with

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

Is there a configuration property that I am missing?

I appreciate the help.

解决方案

You don't need all those annotations to make it working... I would recommend please remove those which are not added purposely by you..!!

To serve your static page on different path than the main context, here is a work-around..!!

Create another simple controller class like below..

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"
    }

}

And, notice here, I have marked the class as @Controller not the @RestController so if you mark it to @RestController or try to do the same in any existing @RestController you would not achieve it easily. So, it's no harm to create another class like above.

Benefit of this way is, it don't destroy your existing mappings.. also the context path is not changes, so no need to bother about your other endpoint paths. They all shall work as before.

Hope this helped!!

这篇关于Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆