对不起,我的冗长代码。 但是我只是一个初学者。我想通过MongoDB学习Spring框架。
我遇到此异常,但我已经阅读了一些有关此问题的文章。但是,我不知道如何解决它。
我还没有创建应用程序配置。这是导致异常的原因吗? 谁能帮我解决这个问题? 我真的很感激。非常感谢...。
package com.mywebapp.dao;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String>{
List<User> findAllUsers();
User findBy_id(ObjectId id);
User findByUsername(String username);
List<User> findByFirstName(String firstName);
List<User> findByLastName(String lastName);
User findByEmail(String email);
List<User> findByDateOfBirth(Date dob);
List<User> findByLocation(Location location);
}
package com.mywebapp.controller;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
import com.mywebapp.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/",method = RequestMethod.GET)
public List<User> getAllUser(){
return userService.findAllUsers();
}
@RequestMapping(value="/{username}",method = RequestMethod.GET)
public User getUserByUserName(@PathVariable("username") String userName){
return userService.findByUserName(userName);
}
@RequestMapping(value="/{firstname}",method = RequestMethod.GET)
public List<User> getUserByFirstName(@PathVariable("firstname") String firstName){
return userService.findByFirstName(firstName);
}
@RequestMapping(value="/{lastname}",method = RequestMethod.GET)
public List<User> getUserByLastName(@PathVariable("lastname") String lastName){
return userService.findByLastName(lastName);
}
@RequestMapping(value="/{email}",method = RequestMethod.GET)
public User getUserByEmail(@PathVariable("email") String email){
return userService.findByEmail(email);
}
@SuppressWarnings("deprecation")
@RequestMapping(value="/{dob}",method = RequestMethod.GET)
public List<User> getUserByDOB(@PathVariable("dob") String dob) throws Exception{
Date dateOfBirth = new Date(dob);
return userService.findByDateOfBirth(dateOfBirth);
}
@RequestMapping(value="/{location}",method = RequestMethod.GET)
public List<User> getUserByLocation(@PathVariable("location") Location location){
return userService.findByLocation(location);
}
@RequestMapping(value= "/{id}", method = RequestMethod.GET)
public User getUserById(@PathVariable("id") ObjectId id) {
return userService.findById(id);
}
}
package com.mywebapp.service;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import com.mywebapp.dao.UserRepository;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
@Service
public class UserService {
private UserRepository userRepo;
public UserService() {
}
public List<User> findAllUsers() {
return userRepo.findAllUsers();
}
public User findById(ObjectId id) {
return userRepo.findBy_id(id);
}
public User findByUserName(String username){
return userRepo.findByUsername(username);
}
public List<User> findByFirstName(String firstName){
return userRepo.findByFirstName(firstName);
}
public List<User> findByLastName(String lastName){
return userRepo.findByLastName(lastName);
}
public User findByEmail(String email){
return userRepo.findByEmail(email);
}
public List<User> findByDateOfBirth(Date dob){
return userRepo.findByDateOfBirth(dob);
}
public List<User> findByLocation(Location location){
return userRepo.findByLocation(location);
}
}
<?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.example</groupId>
<artifactId>MyWebApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyWebApplication</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>10</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</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>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
在资源文件夹下的application.properties
中的属性下面定义,替换您的实际值。如果默认安装了mongodb且没有用户名/密码,则不需要定义用户名和密码属性。 >
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=*********
spring.data.mongodb.password=*********
spring.data.mongodb.database=demo
更新
更新存储库以扩展MongoRepository
@Repository
public interface UserRepository extends MongoRepository<User, ObjectId>
{
从存储库中删除List<User> findAllUsers();
。
将服务中的所有方法更改为以下
public List<User> findAllUsers() {
return userRepo.findAll();
}