Springboot - Resource interpreted as Stylesheet but transferred with MIME type text/htm
I had the exact same issue while writing some code with Spring Boot + Spring MVC. The CSS files set using a CDN worked fine, while the CSS file set from my static/css
folder returned a HTML content.
Example:
<!-- This first worked fine, loading all styles -->
<link th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"
href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet" media="screen" />
<!-- This second one returned the content of an HTML - Content Type text/html -->
<link rel="stylesheet" th:href="@{/css/login/style.css}" href="/css/login/style.css" />
After a while I could see using Chrome Dev Tools that the content returned for my local style.css
was the same as one of my HTML pages.
Inspecting the route that pointed to the HTML file with that content, I could realise I was using the wrong property for the @RequestMapping
configuration. I had @RequestMapping(name="...")
, instead of @RequestMapping(path="...")
.
Controller with the problem
@RequestMapping(name = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());
return "product/edit";
}
Controller changed
@RequestMapping(path = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());
return "product/edit";
}
After changing the property name
for path
everything started being loaded correctly.
It was strange how a small mistake like this affected my whole program.
Hope it's somehow useful for someone who faces the same issue.