Spring数据的其他API MongoDB - 存储库方法不起作用

问题描述:

我正在阅读和学习MongoDB的Spring Boot数据。我在我的数据库中约10个记录的格式如下:Spring数据的其他API MongoDB - 存储库方法不起作用

{ 
    "_id" : ObjectId("5910c7fed6df5322243c36cd"), 
    name: "car" 
} 

当我打开网址:

http://localhost:8090/items 

我得到的所有项目的详尽清单。不过,我想使用的MongoRepositoryfindByIdcount等。当我把它们作为这样的方法:

http://localhost:8090/items/count 
http://localhost:8090/items/findById/5910c7fed6df5322243c36cd 
http://localhost:8090/items/findById?id=5910c7fed6df5322243c36cd 

,我收到了404

我的设置是像这样:

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) throws IOException { 
     SpringApplication.run(Application.class, args); 
    } 
} 

@Document 
public class Item implements Serializable { 
    private static final long serialVersionUID = -4343106526681673638L; 

    @Id 
    private String id; 
    private String name; 

    public String getId() { 
     return id; 
    } 
    public void setId(String id) { 
     this.id = id; 
    } 

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

@RepositoryRestResource(collectionResourceRel = "item", path = "items") 
public interface ItemRepository<T, ID extends Serializable> extends MongoRepository<Item, String>, ItemRepositoryCustom { 

} 

我在做什么错了?我是否需要实施MongoRepository定义的方法,还是会自动实施?我迷路了,一直试图弄清楚这么久。我的控制器中没有任何方法,它是空的。

+1

您是否查看了应用程序启动时查看的请求映射?它会显示所有由Spring Data REST自动创建的映射。 –

+1

对不起。这应该说“当你的应用程序启动时记录” –

+0

谢谢安迪。 Eclipse中是否有一个功能可以告诉我们所有的映射?对不起,我对此不确定。它的工作原理是 – Karthik

您必须声明findById方法才能使其公开。

Item findById(String id); 
Item findByName(String name); 

请注意,您不需要实施这些方法。 SpringBoot将分析方法名称,并提供正确的执行

+0

。但是,计数方法不能以这种方式工作。是因为返回类型长而不是Long? – Karthik

我有同样的问题,

去除@Configuration,@ComponentScan一切正常后。