SpringBootのresourcesフォルダーにあるファイルを取得します
たとえば、header.htmlファイルを取得したい
File file = ResourceUtils.getFile("classpath:templates/header.html");
完全なコード:
@Controller
public class IndexController {
@GetMapping("/header.html")
@ResponseBody
public String header() throws IOException {
File file = ResourceUtils.getFile("classpath:templates/header.html");
String result = FileUtils.readFileToString(file);
System.out.println(result);
return result;
}
}
postmanを使用してリクエスト送信をシミュレートします
結果: