无法映射其余控制器

can't mapping the rest controller

我尝试用 MongoDB 测试 Spring 引导,但是当我 运行 mvn 时,服务器 运行 很好,但在 RestController 中找不到映射

@RestController
@RequestMapping("/mongotest")
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping(value = "/getallusers" , method = RequestMethod.GET)
    public List<User> getAllUser(){
        return service.findAll();
    }
    @RequestMapping(value = "/saveuser" , method = RequestMethod.POST)
    public void saveUser(@RequestBody User user){
        service.createUser(user);
    }
    @GetMapping(value = "/hello")
    public String test(){
        return "hello world";
    }

}
@Repository
public interface UserDAO extends MongoRepository<User,String>{
    List<User> findByName(String name);
    List<User> findByDepartment(String department);
}
@Entity
@Getter @Setter @NoArgsConstructor @ToString @AllArgsConstructor
@Document(collection = "users")
public class User{
    @Id
    private Long id;

    @Field(value = "name")
    private String name;

    @Field(value = "age")
    private Integer age;

    @Field(value = "department")
    private String department;

}
spring.data.mongodb.database= test_mongo
@SpringBootApplication
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.println("Deploy successful");
    }

}

更新服务class

@Service
public class UserService{
    @Autowired
    private UserDAO userDAO;

    public User createUser(User u){
        return userDAO.save(u);
    }

    public List<User> findByName(String name){
        return userDAO.findByName(name);
    }

    public List<User> findByDepartment(String department){
        return userDAO.findByDepartment(department);
    }

    public List<User> findAll(){
        return userDAO.findAll();
    }
}

更新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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

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

</project>

程序运行很好,但在日志中找不到与我在UserController中的RequestMapping的部分映射 当我 运行 映射 link return 404

```Project struct
src
   main
       java
           com
              example
                     demo
                         controller
                                   UserController.java
                         demo
                                   DemoApplication.java
                         entity
                                   User.java
                         repository
                                   UserDAO.java
                         service
                                   UserService.java
       resources
           application.properties
pom.xml
2019-09-09 22:42:05.856  INFO 19036 --- [           main] com.example.demo.demo.DemoApplication    : Starting DemoApplication on DESKTOP-UNR6TSG with PID 19036 (E:\demo\target\classes started by MemeLord in E:\demo)
2019-09-09 22:42:05.861  INFO 19036 --- [           main] com.example.demo.demo.DemoApplication    : No active profile set, falling back to default profiles: default
2019-09-09 22:42:06.782  INFO 19036 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-09-09 22:42:06.784  INFO 19036 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-09 22:42:06.806  INFO 19036 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2019-09-09 22:42:07.549  INFO 19036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-09 22:42:07.588  INFO 19036 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-09 22:42:07.588  INFO 19036 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-09 22:42:07.707  INFO 19036 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-09 22:42:07.708  INFO 19036 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1790 ms
2019-09-09 22:42:08.053  INFO 19036 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-09 22:42:09.060  INFO 19036 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-09-09 22:42:09.138  INFO 19036 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:2}]
to localhost:27017
2019-09-09 22:42:09.144  INFO 19036 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 0]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3475900}
2019-09-09 22:42:09.401  INFO 19036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-09 22:42:09.404  INFO 19036 --- [           main] com.example.demo.demo.DemoApplication    : Started DemoApplication in 4.07 seconds (JVM running for 8.531)
Deploy successful

请提供您的包结构 您要么需要将所有控制器作为 class 的子包,您的 main 方法所在的位置或使用 @ComponentScan 注释。我更喜欢第二种方式,强烈推荐给你

您的包结构有误。您必须将它们放入您的第二个 demo,您的 DemoApplication 所在的位置,而不是您所做的第一个。然后 Spring 会认出他们。

Here 您可以在 Spring Boot.

中阅读有关包结构的信息