package org.thrakt.sampleapp.controllers;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("")
public class HtmlProvideController {
private final ResourceLoader resourceLoader;
public HtmlProvideController(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@GetMapping
public Resource get() {
return resourceLoader.getResource("classpath:html/foobar.html");
}
}
resourceLoader を使って Resource を返す。
@ResponseBody か @RestController で動かす。
/resources 配下はjarの中に入るので、そこから classpath 参照で持ってくる。
モチベーションは
なんか静的HTML返したいけど
/resource/static に置くと直pathで露出してなんか嫌だし
これだけのために Thymeleaf 入れるのも無駄で嫌だ
ということでこれをやった。
うまくビルドスクリプト書けば、別のビルドパイプラインで生み出されたものを同梱して送り出すのもできるかな?