我是春天的新手,试图用mongodb创建一个spring项目。我运行时遇到以下错误。
2018-05-23 22:15:20.077 WARN 11610 --- [main]
ConfigServletWebServerApplicationContext : Exception encountered during
context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'voteController': Unsatisfied dependency
expressed through field 'voteSvc'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'voteService': Unsatisfied dependency expressed
through field 'voteRepo'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'votesRepository': Invocation of init method failed;
nested exception is
org.springframework.data.mapping.PropertyReferenceException: No
property findAll found for type Vote!
2018-05-23 22:15:20.081 INFO 11610 --- [main]
o.apache.catalina.core.StandardService : Stopping service [Tomcat]
这是我的Application.java文件
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@ComponentScan("com.hello")
public class Application implements CommandLineRunner {
@Autowired
private UserRepository userRepo;
private IdeaRepository ideaRepo;
private VotesRepository voteRepo;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
这是我的VoteController.java
package com.hello;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.*;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.boot.autoconfigure.*;
@Controller
@RequestMapping("/api")
public class VoteController{
@Autowired
IVoteService voteSvc;
@RequestMapping(value = "/votes", method = RequestMethod.GET)
public List<Vote> findByIdeaId(int ideaId)
{
List<Vote> votes = voteSvc.GetVotesByIdeaId(ideaId);
return votes;
}
@RequestMapping(value = "/votes", method = RequestMethod.POST)
public Vote PostByIdeaId(Vote vote)
{
Vote votes = voteSvc.SaveVotesByIdeaId(vote);
return votes;
}
@RequestMapping(value = "/votes", method = RequestMethod.DELETE)
public void DeleteByIdeaId(Vote vote)
{
voteSvc.DeleteVotesByIdeaId(vote);
}
}
这是VoteService.java
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import java.util.List;
import java.util.*;
@Service
public class VoteService implements IVoteService {
@Autowired
VotesRepository voteRepo;
public List<Vote> GetVotesByIdeaId(int ideaId)
{
List<Vote> votes = voteRepo.FindAll(ideaId);
//List<Vote> votes = new ArrayList<Vote>();
return votes;
}
public Vote SaveVotesByIdeaId(Vote vote)
{
Vote voteIdea = voteRepo.save(vote);
return voteIdea;
}
public void DeleteVotesByIdeaId(Vote vote)
{
voteRepo.delete(vote);
}
}
这是我的VoteRepository.java
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
@Repository
public interface VotesRepository extends MongoRepository<Vote, String> {
public List<Vote> FindAll(int IdeaId);
}
这是我的界面
package com.hello;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
@Service
public interface IVoteService {
public List<Vote> GetVotesByIdeaId(int ideaId);
public Vote SaveVotesByIdeaId(Vote vote);
public void DeleteVotesByIdeaId(Vote vote);
}
这是我的gradle.build文件
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'idea-platform'
version = '0.1.0'
}
jar {
manifest {
attributes(
'Main-Class': 'hello.Application'
)
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
testCompile("junit:junit")
}
这是我的实体
package com.hello;
import org.springframework.data.annotation.Id;
public class Vote {
@Id
public String id;
public String user;
public Integer ideaId;
public Vote() {}
}
我调查了其他类似的问题,但没有一个能帮我解决。
答案 0 :(得分:0)
您在存储库中定义的方法与Spring Data Repositories方法不匹配。假设ideaId
是Vote
上的属性,找到具有给定Vote
的所有ideaId
的方法应该是这样的:
@Repository
public interface VotesRepository extends MongoRepository<Vote, String> {
public List<Vote> findByIdeaId(int ideaId);
}
有关如何实现Spring Data Repositories的更多详细信息,请查看here。
P.S。:在旁注中,请尝试遵循Java命名约定,以小写字母启动方法和参数名称。
答案 1 :(得分:0)
这不是完整的解决方案,但我也注意到了这个问题。您的每个存储库可能都需要自动装配,而不仅仅是第一个。
你有:
@Autowired
private UserRepository userRepo;
private IdeaRepository ideaRepo;
private VotesRepository voteRepo;
但我想你想要:
@Autowired
private UserRepository userRepo;
@Autowired
private IdeaRepository ideaRepo;
@Autowired
private VotesRepository voteRepo;