Skip to content

Commit 078d09a

Browse files
author
nimuy99
committed
#29 Fix: File -> InputStream 변경
1 parent 6bde112 commit 078d09a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/main/java/com/memesphere/domain/memecoin/entity/MemeCoinInitializer.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.springframework.stereotype.Component;
99

1010
import java.io.File;
11+
import java.io.IOException;
12+
import java.io.InputStream;
1113
import java.util.List;
1214

1315
@Component
@@ -19,14 +21,26 @@ public class MemeCoinInitializer implements CommandLineRunner {
1921
@Override
2022
public void run(String... args) throws Exception {
2123
if (memeCoinRepository.count() == 0) {
22-
String filePath = "src/main/resources/memecoin-storage/memecoin.json";
24+
// JAR 내부 classpath에서 JSON 파일 로드
25+
try (InputStream inputStream = getClass().getClassLoader()
26+
.getResourceAsStream("memecoin-storage/memecoin.json")) {
2327

24-
List<MemeCoin> memeCoins = objectMapper.readValue(
25-
new File(filePath),
26-
new TypeReference<List<MemeCoin>>() {}
27-
);
28+
if (inputStream == null) {
29+
throw new IOException("'memecoin-storage/memecoin.json' 파일을 찾을 수 없습니다.");
30+
}
2831

29-
memeCoinRepository.saveAll(memeCoins);
32+
// JSON 파일을 Java 객체(List<MemeCoin>)로 변환
33+
List<MemeCoin> memeCoins = objectMapper.readValue(
34+
inputStream,
35+
new TypeReference<List<MemeCoin>>() {}
36+
);
37+
38+
// DB에 저장
39+
memeCoinRepository.saveAll(memeCoins);
40+
41+
} catch (IOException e) {
42+
throw new RuntimeException("JSON 파일 로드 중 오류 발생: " + e.getMessage(), e);
43+
}
3044
}
3145
}
3246
}

0 commit comments

Comments
 (0)