我跟着這個tutorial用Spring Boot構建了一個REST服務。一切都編譯好,但啓動後,我得到這個錯誤。用簡單的Spring Boot App獲取錯誤「APPLICATION FAILED TO START」應用程序
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in com.pearlbit.ContactRestController required a bean of type 'com.pearlbit.Contact$ContactRepository' that could not be found.
Action:
Consider defining a bean of type 'com.pearlbit.Contact$ContactRepository' in your configuration.
任何想法是什麼錯?
更新:
這是我的「聯繫」類代碼。
package com.pearlbit;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Document
public class Contact {
public interface ContactRepository extends MongoRepository<Contact, String> {
List<Contact> findByLastName(String lastName);
}
@Id
private String id;
private String firstName;
private String lastName;
private String address;
private String phoneNumber;
private String email;
private String twitterHandle;
private String facebookProfile;
private String linkedInProfile;
private String googlePlusProfile;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTwitterHandle() {
return twitterHandle;
}
public void setTwitterHandle(String twitterHandle) {
this.twitterHandle = twitterHandle;
}
public String getFacebookProfile() {
return facebookProfile;
}
public void setFacebookProfile(String facebookProfile) {
this.facebookProfile = facebookProfile;
}
public String getLinkedInProfile() {
return linkedInProfile;
}
public void setLinkedInProfile(String linkedInProfile) {
this.linkedInProfile = linkedInProfile;
}
public String getGooglePlusProfile() {
return googlePlusProfile;
}
public void setGooglePlusProfile(String googlePlusProfile) {
this.googlePlusProfile = googlePlusProfile;
}
}
我已經包含MongoRepository的接口,但它仍然給我同樣的問題。這是否需要添加到其他地方?
沒有看到你的代碼,但它看起來像ContactRepository沒有註釋爲「@ Component」或「@ Repository」。 請考慮使用官方教程,而不是:https://spring.io/guides/gs/rest-service/ – Kiryl
我加了這些,它仍然給我同樣的錯誤。 – DookieMan
第二個猜測 - 您是否已將註釋@EnableMongoRepositories添加到您的應用程序配置中? – Kiryl