LoginSignup
0
0

More than 3 years have passed since last update.

Spring Boot で static resource と Thymeleaf Engine なしで静的HTMLを提供します。

Posted at
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 入れるのも無駄で嫌だ
ということでこれをやった。
うまくビルドスクリプト書けば、別のビルドパイプラインで生み出されたものを同梱して送り出すのもできるかな?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0