Spring REST控制器未启动

时间:2018-06-25 08:17:36

标签: spring rest

pom.xml

<groupId>com.ibm.springboot</groupId>
<artifactId>spring-boot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-api</name>
<description>Spring API DATA</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.14.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

SpringBootApiApplication.java

@SpringBootApplication
@ComponentScan("com.ibm.springboot.*")
@EntityScan("com.ibm.springboot.topics.Topic")
@EnableJpaRepositories("com.ibm.springboot.topics.TopicRepository")
public class SpringBootApiApplication {

public static void main(String[] args) {
    SpringApplication.run(SpringBootApiApplication.class, args);
}
}

Topic.java

 @Entity
 public class Topic {

 @Id
 private String id;
 private String name;
 private String description;



 public Topic(String id, String name, String description) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
 }
 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;
 }
 public String getDescription() {
    return description;
 }
 public void setDescription(String description) {
    this.description = description;
 }
 }

TopicController.java

@RestController
public class TopicController {

@Autowired
TopicService topicService;

@RequestMapping("/topics")
public List<Topic> getAllTopics(){

    return topicService.getAllTopics();
}

@RequestMapping(method=RequestMethod.POST,value="/topics")
public void addTopic(@RequestBody Topic topic){
    topicService.addTopic(topic);
}
}

TopicRepository.java

@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{

}

TopicService.java

@Service
public class TopicService {

@Autowired
TopicRepository topicRepository;

public List<Topic> getAllTopics(){

    List<Topic> topics = new ArrayList<>();
    topicRepository.findAll().forEach(topics::add);
    return topics;

}

public void addTopic(Topic topic){
//  topics.add(topic);
    topicRepository.save(topic);
 }


 }

运行主类时,出现以下错误。


申请无法开始


  

说明:

     

com.ibm.springboot.topics.TopicService中的字段topicRepository   需要类型为“ com.ibm.springboot.topics.TopicRepository”的bean   找不到。

操作:

考虑在您的配置中定义类型为“ com.ibm.springboot.topics.TopicRepository”的bean。

我已经检查了此处给出的所有相关建议,但未点击任何内容。请帮助

1 个答案:

答案 0 :(得分:0)

@Repository界面中删除TopicRepository批注。 应该是(上面没有注释):

public interface TopicRepository extends CrudRepository{

}

编辑

更改您的:

@EnableJpaRepositories("com.ibm.springboot.topics.TopicRepository")

收件人:

@EnableJpaRepositories(basePackageClasses= {com.ibm.springboot.topics.TopicRepository.class})