Spring Boot,静态资源和mime类型配置 [英] Spring Boot, static resources and mime type configuration
问题描述
我面临无法解决的Spring Boot配置问题... 我正在尝试使用Spring Boot为HbbTV构建一个HelloWorld示例,因此我需要使用mime-type ="application/vnd.hbbtv.xhtml + xml"为我的"index.html"页面提供服务
I'm facing a Spring Boot configuration issue I can't deal with... I'm trying to build an HelloWorld example for HbbTV with Spring Boot, so I need to serve my "index.html" page with mime-type="application/vnd.hbbtv.xhtml+xml"
我的index.html将作为静态页面访问,例如 http://myserver. com/index.html?param = value .
my index.html will be accessed as a static page, for instance http://myserver.com/index.html?param=value.
使用以下代码,无论我多么努力,都会得到一个text/html内容类型.
with the following code, no matter how hard I try, I get a text/html content type.
<?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>
因此,我尝试在@Controller中添加"home()"终结点以强制使用正确的mime类型,并且可以正常工作.
So I tried to add a "home()" endpoint into a @Controller to force the correct mime-type, and that works.
@RestController
public class HbbTVController {
@RequestMapping(value = "/hbbtv", produces = "application/vnd.hbbtv.xhtml+xml")
String home() {
return "someText";
}
...
}
有效"是指码头服务器向我提供一个HTML文件,该文件的内容类型正确,包含测试someText.
"That works" mean the jetty server serves me a html file with the correct content-type containing the test someText.
我的下一个尝试是将@RestController替换为@Controller(相同的 produce 配置),然后将"someText"替换为 index.html
My next try were to replace the @RestController by @Controller (same produce config), and replace "someText" by 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.
Well, it serves my index.html correctly, but the Content-Type is wrong : text/html instead of application/vnd.hbbtv.xhtml+xml. Furthermore, I don't want to access to myserver.com/hbbtv to get index.html, but directly to myserver.com/index.html.
我该怎么做?
谢谢...
推荐答案
好吧,最后,我找到了"Spring Boot兼容解决方案".与Jamie Birch建议的相同,但通过Spring机制实现.
Well, finally, I found the "Spring boot compliant solution". It's the same as Jamie Birch suggested, but realized with Spring mechanisms.
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);
}
}
这篇关于Spring Boot,静态资源和mime类型配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!