I'm new to Spring and after creating a first simple project using the "standard" APIs, I'm trying to integrate everything with Google Firestore using the Spring Cloud GCPs, in particular those related to Firestore (Spring Data Cloud Firestore). But I keep running into this error and I can't figure it out:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in 'com.example.spring_backend.ItemController' required a bean of type 'com.example.spring_backend.ItemRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.spring_backend.ItemRepository' in your configuration.
My project structure is this:
I've put in application.properties the project name and the path to the key of my GC service.
Here is the code of various files:
Item.Java:
@Document(collectionName = "items")
public class Item {
@DocumentId
private String name;
private ArrayList<String> labels;
private Boolean classified;
private String base64;
// Constructors
public Item() {
}
public Item(String name, String base64) {
this.name = name;
this.labels = null;
this.classified = false;
this.base64 = base64;
}
public Item(String name, ArrayList<String> labels, String base64) {
this.name = name;
this.labels = labels;
this.classified = false;
this.base64 = base64;
}
public Item(String name, Boolean classified, ArrayList<String> labels, String base64) {
this.name = name;
this.labels = labels;
this.classified = classified;
this.base64 = base64;
}
public Item(String name, boolean classified, ArrayList<String> labels) {
this.name = name;
this.labels = labels;
this.classified = classified;
this.base64 = null;
}
// Getters and Setters
}
ItemController.java:
@CrossOrigin
@RestController
public class ItemController {
private final ItemRepository itemRepository;
ItemController(ItemRepository repository) {
this.itemRepository = repository;
}
@GetMapping("/items")
public Flux<Item> all() {
System.out.print("ciao");
return itemRepository.findAll();
}
@PostMapping("/save")
public Mono<Item> newEmployee(@RequestBody Item item) {
System.out.print("ciao");
item.setClassified(false);
item.setLabels(new ArrayList<String>());
// ! Da togliere
item.setBase64(null);
return itemRepository.save(item);
}
}
ItemRepository.java:
public interface ItemRepository extends FirestoreReactiveRepository<Item>{
}
SpringBackendApplication.java:
@SpringBootApplication
public class SpringBackendApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBackendApplication.class, args);
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
</parent>
<name>spring_backend</name>
<groupId>com.example</groupId>
<artifactId>spring_backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>3.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-data-firestore</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml after @M.Deinum changes:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
</parent>
<name>spring_backend</name>
<groupId>com.example</groupId>
<artifactId>spring_backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://google.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>4.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-data-firestore</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<updatePolicy>always</updatePolicy>
</project>
Full stack trace:
2023-01-23T15:20:20.257 01:00 INFO 10996 --- [ main] c.e.s.SpringBackendApplication : Starting SpringBackendApplication using Java 17.0.5 with PID 10996 (/home/beppetemp/Tirocinio_NTTData/components/backend/target/classes started by beppetemp in /home/beppetemp/Tirocinio_NTTData)
2023-01-23T15:20:20.263 01:00 INFO 10996 --- [ main] c.e.s.SpringBackendApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-23T15:20:21.510 01:00 INFO 10996 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-01-23T15:20:21.522 01:00 INFO 10996 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-23T15:20:21.522 01:00 INFO 10996 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-01-23T15:20:21.699 01:00 INFO 10996 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-01-23T15:20:21.703 01:00 INFO 10996 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1322 ms
2023-01-23T15:20:21.842 01:00 WARN 10996 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController' defined in file [/home/beppetemp/Tirocinio_NTTData/components/backend/target/classes/com/example/spring_backend/ItemController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.example.spring_backend.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2023-01-23T15:20:21.848 01:00 INFO 10996 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2023-01-23T15:20:21.884 01:00 INFO 10996 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-01-23T15:20:21.969 01:00 ERROR 10996 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.spring_backend.ItemController required a bean of type 'com.example.spring_backend.ItemRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.spring_backend.ItemRepository' in your configuration.
Someone can help me ?
CodePudding user response:
You are using Spring Boot but your spring-cloud-gcp-dependencies
is compatible with Spring Boot 2.6/2.7 and not Spring Boot 3. Hence it doesn't work.
You need to either:
- Downgrade to Spring Boot 2.7.x
- Use the
4.0.0-SNAPSHOT
versions of thespring-cloud-gcp-dependencies
dependencies which are compatible with Spring Boot 3.x.
See also the version compatibility matrix.
For option 2 you will need to add the repository, as mentioned here to your Maven file.
CodePudding user response:
Thanks to M. Deinum's answer I understood that it was a dependency problem, so I created a new project with Spring Initializer adding in Spring GCP dependencies.
The resulting pom.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<name>spring_backend</name>
<groupId>com.example</groupId>
<artifactId>spring_backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<spring-cloud-gcp.version>3.4.2</spring-cloud-gcp.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>${spring-cloud-gcp.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-data-firestore</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>