【问题标题】:Spring Boot, static resources and mime type configurationSpring Boot、静态资源和mime类型配置
【发布时间】:2018-04-23 15:33:02
【问题描述】:

我遇到了一个无法处理的 Spring Boot 配置问题... 我正在尝试使用 Spring Boot 为 HbbTV 构建一个 HelloWorld 示例,因此我需要为我的“index.html”页面提供 mime-type="application/vnd.hbbtv.xhtml+xml"

我的 index.html 将作为静态页面访问,例如 http://myserver.com/index.html?param=value

使用下面的代码,无论我怎么努力,我都会得到一个 text/html 内容类型。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//HbbTV//1.1.1//EN" "http://www.hbbtv.org/dtd/HbbTV-1.1.1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>MyApp HBBTV</title>
    <meta http-equiv="content-type" content="Content-Type: application/vnd.hbbtv.xhtml+xml; charset=UTF-8" />
</head>
<body>
...
</body>
</html>

所以我尝试将“home()”端点添加到@Controller 中以强制使用正确的 mime 类型,这很有效。

@RestController
public class HbbTVController {

    @RequestMapping(value = "/hbbtv", produces = "application/vnd.hbbtv.xhtml+xml")
    String home() {
        return "someText";
    }
...
}

“这有效”意味着码头服务器为我提供了一个包含测试 someText 的内容类型正确的 html 文件。

我的下一个尝试是将@RestController 替换为@Controller(相同的produce 配置),并将“someText”替换为index.html

@Controller
public class HbbTVController {

    @RequestMapping(value = "/hbbtv", produces = "application/vnd.hbbtv.xhtml+xml")
    String home() {
        return "index.html";
    }
...
}

好吧,它正确地为我的 index.html 提供服务,但 Content-Type 错误:text/html 而不是 application/vnd.hbbtv.xhtml+xml。 此外,我不想访问 myserver.com/hbbtv 来获取 index.html,而是直接访问 myserver.com/index.html。

我该怎么做?

谢谢...

【问题讨论】:

    标签: spring spring-mvc spring-boot hbbtv


    【解决方案1】:

    嗯,终于,我找到了“Spring boot compliant solution”。这与 Jamie Birch 建议的相同,但通过 Spring 机制实现。

    Spring Boot 1:

    @Configuration
    public class HbbtvMimeMapping implements EmbeddedServletContainerCustomizer {
    
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
            mappings.add("html", "application/vnd.hbbtv.xhtml+xml; charset=utf-8");
            mappings.add("xhtml", "application/vnd.hbbtv.xhtml+xml; charset=utf-8");
            container.setMimeMappings(mappings);
        }
    
    }
    

    Spring Boot 2:

    @Configuration
    public class HbbtvMimeMapping implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
        @Override
        public void customize(ConfigurableServletWebServerFactory factory) {
            MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
            mappings.add("html", "application/vnd.hbbtv.xhtml+xml; charset=utf-8");
            mappings.add("xhtml", "application/vnd.hbbtv.xhtml+xml; charset=utf-8");
            factory.setMimeMappings(mappings);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我将扩展@Chelute 提供的评论 Sping boot 有默认的 mime 类型 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/MimeMappings.java

      要覆盖已设置的 mime 类型,您应该先将其删除

      这是我用来覆盖 js 和 css 的示例

      @Configuration
      public class CustomServletConfiguration implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
          @Override
          public void customize(ConfigurableServletWebServerFactory factory) {
              MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
              mappings.remove("js");
              mappings.add("js", "application/javascript;charset=utf-8");
              mappings.remove("css");
              mappings.add("css", "text/css;charset=utf-8");
              factory.setMimeMappings(mappings);
              factory.setPort(9000);
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        无法在 Spring Boot 方面提供帮助,但如果您没有得到其他响应,请尝试以下操作:

        • 将文件类型设置为.xhtml而不是.html

        • 在您的 Jetty 服务器的 mime.properties 文件上提供从 .xhtml 到 MIME 类型 application/vnd.hbbtv.xhtml+xml 的映射。关于如何做到这一点的更多细节here

        【讨论】:

          猜你喜欢
          • 2016-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-20
          • 2017-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多