其他包中的@RestController不起作用


问题内容

我从学习Spring开始,我创建了一个基本项目,该项目创建数据库,插入值,然后在Web浏览器中打印它。我的问题是,当我将RestController和主类放在同一个包中时-
可以,但是我想将其分发到其他包中,而当我创建新包时,移动RestController则不起作用。让我们见面解释:

我的项目看起来像:

          |-Springtestv_01
            |-src/main/java
              |--com.person <-- it's a main package
                 |-Main.java
                 |-Person.java
                 |-PersonLineRunner.java
                 |-PersonRepository.java
                 |-PersonController.java
              |-com.controller <-- second package, I want put here PersonController.java
            |-src/main/resources
              |-data.sql
            pom.xml

我的控制器看起来:

@RestController
public class PersonController {

    @Autowired PersonRepository personRepository;

    @RequestMapping("/persons")
    Collection<Person> persons(){
        return this.personRepository.findAll();
    }
}

当一切都在com.person包,我写的网络浏览器的http://本地主机:8080
/人
,它正常工作......但我想移动PersonController.javacom.controller包,当我把它,我webbrowers打电话给我

发生意外错误(类型=未找到,状态= 404)。无可用讯息

而且我不知道该怎么办。也许我应该改变我的东西pom.xml

我的pom.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.person</groupId>
    <artifactId>person</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringTest_v0_1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId><artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>
                spring-boot-starter-data-elasticsearch
            </artifactId>
        </dependency>
    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

它是自动生成的,我只写了一个依赖项

    <dependency>
        <groupId>com.h2database</groupId><artifactId>h2</artifactId>
    </dependency>

问题答案:

使用basePackages:

@ComponentScan(basePackages = { "com.person","com.controller"} )