Home > Software engineering >  Spring Java - Parameter 0 of constructor
Spring Java - Parameter 0 of constructor

Time:01-18

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>

CodePudding user response:

Can you add @Repository annotation to FoodRepository interface.

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

CodePudding user response:

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>
    (...)
</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:

CodePudding user response:

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

  •  Tags:  
  • Related