1. Build. Gradle below
plugins {
Org. Springframework. Id 'boot' version '2.2.6. RELEASE'
Id 'IO. Spring. The dependency - management' version '1.0.9. RELEASE'
Id 'Java'
}
Group='com. Example:'
Version='0.0.1 - the SNAPSHOT'
SourceCompatibility='1.8'
Configurations {
DevelopmentOnly
RuntimeClasspath {
ExtendsFrom developmentOnly
}
CompileOnly {
ExtendsFrom annotationProcessor
}
}
Repositories {
MavenCentral ()
}
Dependencies {
Implementation 'org. Springframework. The boot: spring - the boot - starter - web'
Implementation 'org. Springframework. The boot: spring - the boot - starter - data - directing a'
CompileOnly 'org. Projectlombok: lombok'
AnnotationProcessor 'org. Projectlombok: lombok'
TestImplementation (' org. Springframework. The boot: spring - the boot - starter - test ') {
Exclude group: 'org. Junit. Vintage, module:' junit - vintage - engine '
}
TestImplementation 'org. Springframework. Security: spring ws-security - test'
}
The test {
UseJUnitPlatform ()
}
2. The location error
package com. Example. The demo. The controller;
The import com. Example. Demo. Model. The request. The User;
The import com. Example. Demo. Model. The response. The Result;
The import org. Springframework. Beans. Factory. The annotation. Autowired;
The import org. Springframework. Data. The mongo. Core. MongoTemplate;
The import org. Springframework. Web. Bind. The annotation. PostMapping;
The import org. Springframework. Web. Bind. The annotation. RequestBody;
The import org. Springframework. Web. Bind. The annotation. RestController;
@ RestController
Public class UserController {
The @autowired
Private MongoTemplate MongoTemplate;
@ PostMapping ("/createUser ")
Public Result createUser (@ RequestBody User User) throws the Exception {
MongoTemplate. Save (user);
Result the Result=new Result (" success ");
return result;
}
}
3. An error below
escription:
Field mongoTemplate in com. Example. Demo. Controller. The UserController required a bean of type 'org. Springframework. Data. The mongo. Core. MongoTemplate' that could not be found.
The injection point has The following annotations:
- the @ org. Springframework. Beans. Factory. The annotation. Autowired (required=true)
The following candidates were found but could not be injected:
- Bean method 'mongoTemplate' 'MongoDbFactoryDependentConfiguration' not in the loaded because @ ConditionalOnBean (types: org. Springframework. Data. The mongo. MongoDbFactory; SearchStrategy: all) did not find any beans of type org. Springframework. Data. The mongo. MongoDbFactory
Action:
Consider revisiting the entries above or defining a bean of type 'org. Springframework. Data. The mongo. Core. MongoTemplate' in your configuration.