Friday, January 25, 2019

SpringBoot에서 Gradle Bootjar로 패키징 시 발생하는 Extra File Path 오류 수정 방법

SpringBoot에서 추가 파일 경로 설정과 파일 읽기 방법

이전에 SpringBoot 환경에서 추가파일의 위치 경로를 설정해서 파일을 읽는 방법에 대해 포스팅을 했습니다. 하지만 해당 방법으로 jar나 war파일로 패키징하여 배포할 때, 파일을 찾지 못하는 문제가 발생하였습니다.

일반적인 file system에서의 경로와 패키징 됐을 때의 경로가 달라지는 듯한 느낌이 들어, 패키징 할 때와 일반 소스를 띄울때와 경로설정을 다르게 해야하는 것인가? 라는 의문이 들었습니다. 그러던 중 구글링을 통해 해결방법을 찾았습니다.

기존에 사용하던 ResourceUtils.getFile(path) 방식 대신 new ClassPathResource(path)를 사용하여 리소스 객체를 생성 후 getInputStream 메서드를 사용하면 됩니다.

실제 적용 예시 코드

@PostConstruct
public void init() throws IOException {
   Resource resource = new ClassPathResource("your-firebase.json");
   FirebaseOptions options = new FirebaseOptions.Builder()
          .setCredentials(GoogleCredentials.fromStream(resource.getInputStream()))
          .build();
   FirebaseApp.initializeApp(options);
}

0 개의 댓글:

Post a Comment