简体   繁体   中英

Data from mongodb when using Spring boot

I am using Spring boot with spring data. I have a mongodb and has the following record inside it:

{ "_id" : ObjectId("5914281fae0fe519943367c7"), "_class" : "example.model.Customer", "username" : "test", "password" : "test", "message" : "test" }

I am trying to create a function in which it retrieves data from the record using their username and password. This is what I have so far:

public class findExistingUser {
@Autowired
MongoTemplate mongoTemplate;
public Customer findUser(String username, String password){
    Query query = new Query();
    query.addCriteria(Criteria.where("username").is(username).and("password").is(password));
    return mongoTemplate.findOne(query, Customer.class, "customer");
}
}

But for some reason I get a null pointer exception. When I debug, mongotemplate seems to be null.

Below properties should be available in application.properties

spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=app1

And below dependecies should be available in pom

org.springframework.data:spring-data-mongodb:1.10.0.RELEASE org.mongodb:mongodb-driver:3.4.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM