springboot 读取 json 文件

版权声明:本文为博主原创文章,未经允许不得转发 https://blog.csdn.net/fengchen0123456789/article/details/86508543

直接代码,json文件在 resources 下

import com.yf.af.config.controller.IServerConfigController
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import sun.misc.IOUtils

@RestController
class ServerConfigController: IServerConfigController {

    // 加载 resource 下的 json文件
    @Value("classpath:static/config.json")
    private  lateinit var resource: Resource

    @GetMapping("/config")
    override fun findServerConfig(): ResponseEntity<String> {

        val json = String(IOUtils.readFully(resource.getInputStream(), -1, true))

        return ResponseEntity.ok(json)
    }
}

猜你喜欢

转载自blog.csdn.net/fengchen0123456789/article/details/86508543