2017-03-01 33 views
0

我跟着這個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的接口,但它仍然給我同樣的問題。這是否需要添加到其他地方?

+0

沒有看到你的代碼,但它看起來像ContactRepository沒有註釋爲「@ Component」或「@ Repository」。 請考慮使用官方教程,而不是:https://spring.io/guides/gs/rest-service/ – Kiryl

+0

我加了這些,它仍然給我同樣的錯誤。 – DookieMan

+0

第二個猜測 - 您是否已將註釋@EnableMongoRepositories添加到您的應用程序配置中? – Kiryl

回答

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實施。

+0

我有這是我的模特班。 公共接口ContactRepository擴展了MongoRepository {} 它屬於那裏還是別的地方? – DookieMan

+0

您的界面嵌套在Componet類中。您可以嘗試將接口提取到單獨的java文件並添加@Repository –

+0

這是正確的。我的問題是我沒有在單獨的文件中有這個。 – DookieMan

0

可能你正在使用的bean類型是Contact $ ContactRepository,但是你還沒有創建它,你應該首先註冊一個bean類型。如果你使用的是spring引導,你可以通過添加JPA starter來完成擴展了JPA Repository接口

+1

你能舉一個例子嗎?或者你能詳細說明你爲什麼認爲這種情況? – liorsolomon

相關問題