我试图用MongoDB测试Spring Boot,但是当我运行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");
}
}
更新服务类别
@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的零件映射 当我运行映射的链接时,返回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
答案 0 :(得分:1)
您的软件包结构错误。您必须将它们放入第二个demo
,而您的DemoApplication
却不在您的第一个。然后,Spring将识别它们。
Here,您可以阅读有关Spring Boot中包结构的信息。
答案 1 :(得分:0)
请提供您的包裹结构
您可能需要将所有控制器作为main
方法所在的类的子包,或者使用@ComponentScan
注释。我更喜欢第二种方式,强烈推荐给您