【发布时间】:2021-09-22 12:31:53
【问题描述】:
我正在将 Docker 用于 Spring 启动应用程序。我想将 JSON 文件用于我的 Google 令牌身份验证。虽然我在我的机器上本地运行它,但一切正常。但是当我在 docker 环境中运行它时,出现以下异常:
java.io.FileNotFoundException: class path resource [file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/file.json
这个file.json 位于/src/main/resources/file.json 获取这个文件我使用下面的代码:
@Throws(FileNotFoundException::class, IOException::class)
fun getAccessToken(): AccessToken? {
val resource = ClassPathResource("file.json")
val inputStream: InputStream = resource.inputStream
val credentials: GoogleCredentials = GoogleCredentials.fromStream(
inputStream
).createScoped(
"https://www.googleapis.com/auth/...",
"https://www.googleapis.com/auth/..."
)
return credentials.accessToken
}
我也尝试使用以下方法。
@Value("classpath:/file.json")
lateinit var resource: Resource
@Throws(FileNotFoundException::class, IOException::class)
fun getAccessToken(): AccessToken? {
val inputStream: InputStream = resource.inputStream
val credentials: GoogleCredentials = GoogleCredentials.fromStream(
inputStream
).createScoped(
"https://www.googleapis.com/auth/...",
"https://www.googleapis.com/auth/..."
)
两种方法都抛出相同的异常。任何评论都将不胜感激。
【问题讨论】:
标签: spring-boot docker kotlin