Stworzyłem aplikację Spring Boot z połączeniem z bazą danych postgres i nazwą pracownika jako nazwą bazy danych i pracownikami jako nazwą tabeli. Stworzyłem kilka klas kontrolera, modelu i dao, które są pokazane poniżej, a wszystkie zależności są również dodane. Ale podczas uruchamiania aplikacji pojawia się następujący błąd:

2020-04-02 02:29:11.681  INFO 14940 --- [           main] c.e.d.employe_db.EmployeDbApplication    : Starting EmployeDbApplication on LAPTOP-JQ3KG74S with PID 14940 (C:\Users\ravik\IdeaProjects\employe_db\target\classes started by ravik in C:\Users\ravik\IdeaProjects\employe_db)
2020-04-02 02:29:11.695  INFO 14940 --- [           main] c.e.d.employe_db.EmployeDbApplication    : No active profile set, falling back to default profiles: default
2020-04-02 02:29:12.575  INFO 14940 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-02 02:29:12.575  INFO 14940 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-02 02:29:12.575  INFO 14940 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-02 02:29:12.637  INFO 14940 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-02 02:29:12.637  INFO 14940 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 890 ms
2020-04-02 02:29:12.659  WARN 14940 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.eployees.data.employe_db.dao.EmployeeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-04-02 02:29:12.659  INFO 14940 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-04-02 02:29:12.674  INFO 14940 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-02 02:29:12.737 ERROR 14940 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field employeeRepository in com.eployees.data.employe_db.controller.EmployeeController required a bean of type 'com.eployees.data.employe_db.dao.EmployeeRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.eployees.data.employe_db.dao.EmployeeRepository' in your configuration.


Process finished with exit code 1

EmployeeDetails.java

package com.eployees.data.employe_db.model;
import javax.persistence.*;
@Entity
@Table(name = "employees")
public class EmployeeDetails {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "NAME")
String name;

@Column(name = "AGE")
int age;

@Column(name = "ADDRESS")
String address;

@Column(name = "SALARY")
double salary;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

@Override
public String toString() {
    return "EmployeeDetails{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", age=" + age +
            ", address='" + address + '\'' +
            ", salary=" + salary +
            '}';
}

}

EmployeeRepository.java

package com.eployees.data.employe_db.dao;

import com.eployees.data.employe_db.model.EmployeeDetails;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeDetails,Integer> {
    Optional<Object> findAllById(Long employeeId);
}

EmployeeController.java

package com.eployees.data.employe_db.controller;

import com.eployees.data.employe_db.exception.ResourceNotFoundException;
import com.eployees.data.employe_db.dao.EmployeeRepository;
import com.eployees.data.employe_db.model.EmployeeDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    //get employees
    @GetMapping("employees")
    public List<EmployeeDetails> getAllEmployee(){
        return this.employeeRepository.findAll();
    }
    //get employee by id
    @GetMapping("/employees/{id}")
    public ResponseEntity<EmployeeDetails> getEmployeeById(@PathVariable(value = "id") Long employeeId) throws ResourceNotFoundException {
        EmployeeDetails employee = (EmployeeDetails) employeeRepository.findAllById(employeeId).
                orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id:: "+ employeeId));
        return ResponseEntity.ok().body(employee);
    }
    //save employee
    @PostMapping("employees")
    public EmployeeDetails createEmployee(@RequestBody EmployeeDetails employee){
        return this.employeeRepository.save(employee);
    }

    //update employee
    @PutMapping("employees")
    public ResponseEntity<EmployeeDetails> updateEmployee(@PathVariable(value = "id") Long employeeId, @Valid @RequestBody EmployeeDetails employeeDetails) throws ResourceNotFoundException {
        EmployeeDetails employee = employeeRepository.findById(Math.toIntExact(employeeId)).orElseThrow(()-> new ResourceNotFoundException("Employee not found for this id:: "+ employeeId));
        employee.setName(employeeDetails.getName());
        employee.setAge(employeeDetails.getAge());
        employee.setAddress(employeeDetails.getAddress());
        employee.setSalary(employeeDetails.getSalary());
        return ResponseEntity.ok(this.employeeRepository.save(employee));
    }

    //delete employee
    @DeleteMapping("employees")
    public Map<String,Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId) throws ResourceNotFoundException {
        EmployeeDetails employee = employeeRepository.findById(Math.toIntExact(employeeId)).orElseThrow(()-> new ResourceNotFoundException("Employee not found for this id:: "+ employeeId));

        this.employeeRepository.delete(employee);

        Map<String,Boolean> response = new HashMap<>();
        response.put("deleted",Boolean.TRUE);
        return response;
    }

}

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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.eployees.data</groupId>
    <artifactId>employe_db</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>employee_db</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- PostgreSQL -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/employee
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.show-sql=true

#drop n create table again, good for testing, comment this in production
spring.jpa.hibernate.ddl-auto=create

EmployeDbApplication.java

package com.eployees.data.employe_db;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EmployeDbApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeDbApplication.class, args);
    }

}

ErrorDetails.java

package com.eployees.data.employe_db.exception;

import java.util.Date;

public class ErrorDetails {
    private Date timestamp;
    private String message;
    private String details;

    public ErrorDetails(Date timestamp, String message, String details) {
        super();
        this.timestamp = timestamp;
        this.message = message;
        this.details = details;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }
}

GlobalExceptionHandler.java

package com.eployees.data.employe_db.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import java.util.Date;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request){
        ErrorDetails errorDetails = new ErrorDetails(new Date(),ex.getMessage(),request.getDescription(false));
        return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> globalExceptionHandler(Exception ex , WebRequest request){
        ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),request.getDescription(false));
        return new ResponseEntity<>(errorDetails,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

ResourceNotFoundException.java

package com.eployees.data.employe_db.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception {

    private static final long serialVersionUid = 1L;

    public ResourceNotFoundException(String message){
        super(message);
    }

}

Struktura projektu tutaj wprowadź opis obrazu

Oto kilka sugestii, które otrzymuję od IDE. tutaj wprowadź opis obrazu

Problem z dodaniem nowego rekordu w metodzie postu. tutaj wprowadź opis obrazu

Nie mogłeś dowiedzieć się, dlaczego to się dzieje? Spróbuj w tym pomóc.

0
Ravi Kumar 2 kwiecień 2020, 00:25

3 odpowiedzi

Najlepsza odpowiedź

Problem polega na tym, że zdefiniowałeś spring-boot-starter-data-jpa tak, aby był dostępny tylko w zakresie test, ale potrzebujesz go w swojej zwykłej aplikacji! Po prostu usuń tag scope.

2
chrylis -cautiouslyoptimistic- 1 kwiecień 2020, 22:23

Repozytorium to interfejs.

Spójrz na to pytanie i zobacz, czy udzielona tam odpowiedź pomoże ci

Jak działa interfejs @Autowired, który rozszerza CrudRepository? Chciałbym mieć pewne spostrzeżenia

0
Jkike 1 kwiecień 2020, 21:56

Spróbuj dodać @EnableJpaRepositories do EmployeDbApplication.java

0
James King 1 kwiecień 2020, 21:54