Skip to content

Commit

Permalink
Merge pull request #19 from Dae-Hwa/BE/error-fix/cannot-find-jsonfile
Browse files Browse the repository at this point in the history
[FIX] : 배포 시 JSON파일 읽을 수 없는 문제 수정
  • Loading branch information
Dae-Hwa authored Apr 23, 2021
2 parents c812856 + 0e34823 commit 7ceb486
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.codesquad.sidedish.utils;

import com.codesquad.sidedish.web.sidedish.DTO.DetailDTO;
import com.codesquad.sidedish.web.sidedish.DTO.ItemDTO;
import com.codesquad.sidedish.web.sidedish.DTO.SidedishDTO;
import com.codesquad.sidedish.web.sidedish.DTO.DetailDTO;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -56,12 +57,12 @@ private static <E> E jsonToObject(String jsonFilePath, TypeReference<E> typeRefe
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

try {
File file = new ClassPathResource(jsonFilePath).getFile();

return objectMapper.readValue(file, typeReference);
try (InputStream is = new ClassPathResource(jsonFilePath).getInputStream()) {
return objectMapper.readValue(is, typeReference);
} catch (JsonParseException e) {
throw new IllegalStateException("Json파일 파싱 중 에러 발생. jsonFilePath: " + jsonFilePath, e);
} catch (IOException e) {
throw new IllegalStateException("경로가 올바르지 않습니다. 경로: " + jsonFilePath, e);
throw new IllegalStateException("파일 읽는 중 에러 발생. jsonFilePath: " + jsonFilePath, e);
}
}
}

0 comments on commit 7ceb486

Please sign in to comment.