1

I'm trying to write a simple CRUD program and I get this error. The program is based after codecademy project. Not sure why I doesn't work.

If I comment out the constructor the error disappears.I don't have anything in my properties.

Can someone give me a hand?

Description: Parameter 0 of constructor in com.example.FitApp3.controller.FoodController required a bean of type 'com.example.FitApp3.repository.FoodRepository' that could not be found. Action: Consider defining a bean of type 'com.example.FitApp3.repository.FoodRepository' in your configuration. Process finished with exit code 1 

This is my code:

Entity/Food.java

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Food extends com.example.FitApp3.model.Entity { private String foodName; private int foodKcal; private int foodProtein; private int foodCarb; private int foodFat; public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; } public int getFoodKcal() { return foodKcal; } public void setFoodKcal(int foodKcal) { this.foodKcal = foodKcal; } public int getFoodProtein() { return foodProtein; } public void setFoodProtein(int foodProtein) { this.foodProtein = foodProtein; } public int getFoodCarb() { return foodCarb; } public void setFoodCarb(int foodCarb) { this.foodCarb = foodCarb; } public int getFoodFat() { return foodFat; } public void setFoodFat(int foodFat) { this.foodFat = foodFat; } } 

Repository/FoodRepository.java

public interface FoodRepository extends CrudRepository<Food, Integer> {} 

Controller/FoodController.java

@RestController public class FoodController { private FoodRepository foodRepository; public FoodController(FoodRepository foodRepository) { this.foodRepository = foodRepository; } } 

Mainclass

@SpringBootApplication public class FitApp3Application { public static void main(String[] args) { SpringApplication.run(FitApp3Application.class, args); System.out.println("hello world"); } } 

pom.xml

<?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.6.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>FitApp3</artifactId> <version>0.0.1-SNAPSHOT</version> <name>FitApp3</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>2.5.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
4
  • Can you please add your main class and also your pom.xml? Thanks! Commented Jan 17, 2022 at 22:32
  • I did. Thx for the quick reply.. Commented Jan 17, 2022 at 22:38
  • An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.data.jpa.mapping.JpaPersistentPropertyImpl.getPersistentEntityTypeInformation(JpaPersistentPropertyImpl.java:145) The following method did not exist: 'java.lang.Iterable org.springframework.data.mapping.model.AnnotationBasedPersistentProperty.getPersistentEntityTypeInformation()' I tried to reimport everything but no luck. Commented Jan 17, 2022 at 23:05
  • Add spring-boot-starter-data-jpa remove the spring-data-commons and javax.persistence-api dependencies. Commented Jan 18, 2022 at 6:25

4 Answers 4

0

Can you add @Repository annotation to FoodRepository interface.

@Repository public interface FoodRepository extends CrudRepository<Food, Integer> {} 
Sign up to request clarification or add additional context in comments.

5 Comments

I did, didn't work.
So, can you please provide package name FitApp3Application?
com.example.FitApp3
Can you also try it with "spring-boot-starter-data-jpa" dependency (that João mentioned).
Adding @Repository to a Spring Data repository doesn't add anything.
0

You need to replace spring-boot-starter-jdbc with spring-boot-starter-data-jpa as follows:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 

You can have a look at the following links the difference between them, but basically, JPA helps you deal with your Database data by mapping it directly to Java objects:

3 Comments

Now all my annotations don't work and importing doesn't work.
Sorry, my bad. I've fixed the dependency name. Please check it ;)
@cyze, I've updated my answer, please check it. Thanks!
0

I think you are just a little bit confused with spring-jdbc, spring-data and spring persistence interfaces abstraction, let me help. TL;DR solution is at the bottom:

In the constructor of FoodController you are declaring FoodRepository as a parameter, so spring have to find the bean of this time at runtime in order to create bean of FoodController. You have declared FoodRepository and extend it from CrudRepository. I guess, that you have done it with the assumption, that spring will create an implementation of FoodRepository at runtime (because it extends CrudRepository). But, unfortunately, it is not the spring-core module, that will create bean of FoodRepsitory, nor it is spring-jdbc. This interfaces are the part of spring-data project. So, for creation of the bean of the type FoodRepository is responsible current spring-data project in your classpath (I mean, it could be spring-data-jdbc, spring-data-jpa or whatever). This interfaces (CrudRepository, Repository e.t.c.) are common for all of the spring data projects, so they are shipped in spring-data-commons jar, this is why you have them available in classpath, because you have included spring-data-commons explicitly:

 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>2.5.1</version> </dependency> 

Another thing is that this starter:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 

does not do anything with spring-data project - it just brings spring-jdbc, Hikari connection pool, and some other spring-boot stuff. In other words, the problem is that you do not have any spring-data project in your classpath, thats why FoodRepository bean is not created.

Also note: @Entity does not make sense here, becuase you do not have any jpa persistence provider in classpath

Solution:

To solve the problem, I suggest you to

  1. Remove spring-data-commons dependency from pom.xml
  2. Include spring-boot-starter-data-jpa into your project (into pom.xml). It will bring the appropriate version of spring-data-commons dependency.

Hope it helped, have a nice day!

Comments

-1

Add @Repository annotation to FoodRepository interface and also @Autowired annotation to the FoodRepository in FoodController.

1 Comment

Adding that to the Spring Data repository adds nothing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.