我开始学习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
包里时,我在Web浏览器中输入http://localhost:8080/persons,它可以正常工作...
但是,我想把PersonController.java
移动到com.controller
包中,在我移动它之后,我的浏览器会显示:
There was an unexpected error (type=Not Found, status=404). No message available
我不知道应该怎么修复它。也许我应该更改我的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>
com.person
定义了一个组件扫描,但没有为其他包定义。 - hotzstpom.xml
文件中。 - CeZet