spring 资源文件夹中的文件对于docker文件不可见

xoefb8l8  于 2023-04-04  发布在  Spring
关注(0)|答案(1)|浏览(117)

我有一个Spring Boot 应用程序,我正在消耗外部soap服务,我有要求把wsdl文件本地,我把它放在wsdl文件夹下的资源文件夹。我正在阅读该文件是在wsdl文件夹下的资源文件夹-

ClassPathResource resource = new ClassPathResource("wsdl/custom.wsdl");
File file = resource.getFile();
wsdlURL = file.toURI().toURL();

我已经创建了一个简单的Dockerfile,包含以下内容并运行Sping Boot 应用程序

FROM openjdk:8-jdk-alpine
ADD target/*.jar /app/app.jar
CMD ["java", "-jar", "/app/app.jar"]
EXPOSE 8080

但我得到下面的错误。

java.io.FileNotFoundException: class path resource [wsdl/custom.wsdl] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app/app.jar!/BOOT-INF/classes!/wsdl/custom.wsdl
qv7cva1a

qv7cva1a1#

尝试这种方法,它也可以在docker镜像中工作。

import com.fasterxml.jackson.core.type.TypeReference;

InputStream inputStream = TypeReference.class.getResourceAsStream("wsdl/custom.wsdl");

StringBuffer sb = new StringBuffer();
int i=0;
while((i=inputStream.read())!=-1) {
   sb.append((char)i);
}
System.out.println(new String(sb));

在我的情况下,我也面临着同样的问题。Docker镜像无法找到资源文件夹中的文件,但上述解决方案解决了我的问题。

相关问题