After hours of reading different posts, example projects, and many different fixes, I solved an issue related to serving static content within my Spring Boot project- specifically errors I received from a link to a CSS in an html page. It was the less popular of two answers in this following link (excerpt below) that solved it for me.
Check the Controller classes for which annotation is used - @RestController or @Controller. Do not mix Rest API and MVC behaviour in one class. For MVC use @Controller and for REST API use @RestController
Spring Boot app not serving static content
In summary, after everything I tried (modifying application.properties to explicitly set static directory paths, moving the CSS file into many different folders, including the supposed standard set ones) the style sheet could not be found. BUT...Changing out @RestController for @Controller solved it for the following code.
@Controller
public class MVCController {
@Autowired
ThingdefstatsviewRepository defstatviewRepository;
@Autowired
ThingDefRecord thingDefRecord;
@GetMapping(path="/characterdef/{defid}")
public ModelAndView thymeLeafCharacterDefByID(@PathVariable String defid) {
Long id = Long.parseLong(defid);
thingDefRecord.loadAllStats(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("CharacterPage.html");
//Adds an attribute to the model as a name/value(object) pair
modelAndView.addObject("thingstats", thingDefRecord);
return modelAndView;
}
}
Question is, why? What is it about using a ModelAndView object in a @RestController class that prevented my html page from loading the CSS file? Or the reverse, what is it about simply using @Controller that made this instantly work?
Clearly, as a novice with Spring, I am missing something about how these different Controllers function with linked static content, but can't find out why.