简体   繁体   中英

Accessing files in the resource folder when the app is running on the server in a spring boot app

I added Firebase auth to a spring boot project. For me to use the firebase resources, I have to add a json file that have the token for initialization of the firebase. The code for the initialization looks like this.

try {


            InputStream serviceAccount =
                    PplusserverApplication.class.getResourceAsStream("/xxxxxx.json");
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://xxxxxxxxxxxxxxxxx")
                    .build();


            FirebaseApp.initializeApp(options);
            FirebaseApp.getInstance().getName();
            logger.info("Is Firebase Started :"+FirebaseApp.getInstance().getName());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("stack: "+e.toString());
        }

It runs fine on the laptop and on tomcat locally. The problem comes when I deploy it to the sever. When I make a request to it I get this error "FirebaseApp with name [DEFAULT] doesn't exist. ".

What I have tried:

  1. I moved the json file to the static folder.

2)I added @EnableWebMvc to add the resources folder and have access to it.

  1. I ran mvn generate resources when building the project.

Doing all this still gave me the same error "FirebaseApp with name [DEFAULT] doesn't exist. "

I am deploying a war file on tomcat8 on aws ec2

How do I add the json file to the war and have access to it? Or how would you solve this problem. Thanks in advance

It will help you to read from folder in spring boot application:

  Resource resource = new ClassPathResource("classpath:XXXX.json");
  InputStream inputStream = resource.getInputStream();

Also for resources by autowiring @Autowired ResourceLoader resourceLoader

Resource resource = 
resourceLoader.getResource("classpath:XXXX.json");
InputStream inputStream = resource.getInputStream();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM