我正在开发Spring Boot应用程序。尝试了许多解决方案后,它仍未解决。请帮助:
控制台:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'doctorController'的bean时出错:通过字段'doctorService'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'doctorService'的bean时出错:通过字段'doctorRepo'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'doctorRepository'的bean时出错:调用init方法失败;嵌套的异常是org.springframework.data.mapping.PropertyReferenceException:找不到类型为Doctor的属性ID!
原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'doctorService'的bean时出错:通过字段'doctorRepo'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'doctorRepository'的bean时出错:调用init方法失败;嵌套的异常是org.springframework.data.mapping.PropertyReferenceException:找不到类型为Doctor的属性ID!
Controller:
public class DoctorController {
@Autowired
private DoctorService doctorService;
@RequestMapping("/hms/doctor")
public List<Doctor> getAllDoctor()
{
return doctorService.getAllDoctor();
}
@RequestMapping(method=RequestMethod.POST,value="hms/doctor")
public void addDoctor(@RequestBody Doctor doctor)
{
doctorService.addDoctor(doctor);
}
} ```
**Service class:**
```@Service
public class DoctorService {
Logger logger= LoggerFactory.getLogger(DoctorService.class);
@Autowired
private DoctorRepository doctorRepo;
public List<Doctor> getAllDoctor(){
logger.error("error happened");
logger.trace(" Error !!!");
List<Doctor> doctor= new ArrayList<Doctor>();
doctorRepo.findAll().forEach(doctor::add);
return doctor;
}
public Doctor getDoctorById(String doctorId) {
return doctorRepo.findById(doctorId);
}
public Doctor getDoctorByPhoneNumber(long phoneNumber)
{
return doctorRepo.findByPhoneNumber(phoneNumber);
}
public void addDoctor(Doctor doctor) {
doctorRepo.save(doctor);
}
public void updateDoctor(String doctorId,Doctor doctor) {
doctorRepo.save(doctor);
}```
**Repository:**
```public interface DoctorRepository extends CassandraRepository<Doctor, Integer>
{
Doctor findById(String doctorId);
Doctor findByPhoneNumber(long phoneNumber);
void deleteById(String doctorId);
}```
**Model class:**
`@Entity
public class Doctor {
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@PrimaryKey
private int doctorId;
private String doctorName;
private long phoneNumber;
private String specialization;
private int totalExperience;
private String workingDays[]= {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
public Doctor() {
}
public Doctor(int doctorId, String doctorName, long phoneNumber, String specialization, int totalExperience,
String[] workingDays) {`enter code here`
super();
this.doctorId = doctorId;
this.doctorName = doctorName;
this.phoneNumber = phoneNumber;
this.specialization = specialization;
this.totalExperience = totalExperience;
this.workingDays = workingDays;
// getters and setters `
**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.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.fullstack.assessment</groupId>
<artifactId>hms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hms</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>`
医生的ID是doctorId,所以
findById(...
deleteById(...
应该是
findByDoctorId(...
deleteByDoctorId(...
或使用getter和setter将doctorId
更改为简单的id
。>>