我按照这个tutorial使用Spring Boot构建REST服务。一切都很好,但在启动后我得到了这个错误。
***************************
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的界面,但它仍然给我同样的问题。这是否需要在其他地方添加?
答案 0 :(得分:0)
可能你使用的bean类型是Contact $ ContactRepository,但是你还没有创建它,你应该首先注册这种类型的bean。如果你使用的是spring boot,你可以通过添加JPA启动器来完成扩展JPA Repository接口
答案 1 :(得分:0)
package hello;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ContactRepositoryextends ContactRepository<Contact, Long> {
List<Customer> findByLastName(String lastName);
}
试试这个。您的存储库将由spring自动实现。