I'm working on a Spring Boot application and I'm trying to connect to Google BigQuery using the Google Cloud Client Library. I have a service account JSON credentials file located at src/main/resources/my-todo/vm-dev-393108-80d765f9af09.json in my project.
I've defined the path to my credentials in application.properties like so:
myapp.google.credentials.path=classpath:vm-dev-393108-80d765f9af09.json
And I use this path to load the credentials in my Spring configuration:
@Configuration
public class BigQueryConfig {
@Value("${myapp.google.credentials.path}")
private String googleCredentialsPath;
@Bean
public GoogleCredentials googleCredentials() {
try (FileInputStream serviceAccountStream = new FileInputStream(googleCredentialsPath)) {
return GoogleCredentials.fromStream(serviceAccountStream);
} catch (IOException e) {
throw new RuntimeException("Failed to load BigQuery credentials", e);
}
}
@Bean
public BigQuery bigQuery(GoogleCredentials credentials) {
return BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
}}
However, when I run my application, I'm encountering the following error:
java.io.FileNotFoundException: class path resource [vm-dev-393108-80d765f9af09.json] cannot be opened because it does not exist
I've checked the file path multiple times and it appears to be correct. I'm not sure why Spring is unable to locate my JSON file.
Any help would be greatly appreciated. Thank you!
I am a new Spring Boot developer and this is my first time trying to establish a connection to Google's BigQuery from a Spring Boot application. I am developing my application in Gitpod, an online development environment.
So far, I have tried different ways to define the path to my JSON credentials file:
I initially used the full path, like my-todo/src/main/resources/vm-dev-393108-80d765f9af09.json, but it didn't work.
I then tried copying the relative path from the context menu in my IDE and used that path in my application.properties file, but the application still could not locate the JSON file.
My expectation is that when I run my Spring Boot application, it should be able to use the provided path to locate the JSON file, load the credentials, and connect to BigQuery successfully.
However, regardless of how I define the path to my JSON file, I keep encountering a FileNotFoundException stating that the JSON file does not exist, even though the file does indeed exist in my project.
I'm not sure if there's something specific about Spring, Gitpod, or my relative inexperience that is causing this issue. I suspect there might be some limitations or specificities in Gitpod's workspace that prevent it from accessing a JSON file located within the workspace itself. I am eager to understand the mistake I'm making and learn how to correct it.
Remember, the more context and details you provide, the easier it will be for others to help you!