am newbie to spring boot am trying to create a simple application using spring boot am getting the error as:
org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name'carsApplication':Unsatisfied dependency expressed through field'carMongoRepository';
nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'carMongoRepository':Invocation of init method failed;nested exception is org.springframework.data.mapping.model.MappingException:Could not lookup mapping metadata for domain class java.lang.Object!
This is the Model class that I had implemented:
Model class for cars having id,make and model and it includes get and set method
package com.example.cars;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;
@Document(collection = "cars")
public class Car {
private String id;
private String make;
private String model;
public Car() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
This is the Main class that I had implemented:
Main class for the spring boot application to run the application
package com.example.cars;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class CarsApplication {
@Autowired
CarMongoRepository carMongoRepository;
public static void main(String[] args) {
SpringApplication.run(CarsApplication.class, args);
}
}
This is the Controller class that I had implemented:
Controller class for the API calls to add the car values like model and make.Include API call as shown below having the POST method implementation.
package com.example.cars;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CarController {
@Autowired
CarMongoRepository carMongoRepository;
@RequestMapping(value = "/addCar", method = RequestMethod.POST)
public void addCar(@ModelAttribute Car car) {
carMongoRepository.save(car);
}
}
This is the repository that I had implemented
Implemented interface for the CRUD repository call for the application as shown below.Used @Repository annotation for this implementation. Class name is CarMongoRepository that extends CRUDRepository as shown below.
package com.example.cars;
import org.springframework.data.repository.CrudRepository;
import com.example.cars.Car;
import org.springframework.stereotype.Repository;
@Repository
public interface CarMongoRepository extends CrudRepository {
}
While am try to run the spring boot main class am getting the error as mentioned above.So, Please help me to solve this issue.
CrudRepository needs generic parameters to know which Entity (or Document) it should handle.
Make your CarMongoRepository
extend CrudRepository<Car, String>
and it should work.
The first parameter specifies the type of entity/document the repository should handle and the second parameter is the type of the id of this entity/document (i assume that it is the field id
of type String
inside your Car class)
If you dont specify any parameters it assumes your CarMongoRepository
to extend CrudRepository<Object, Object>
and that is what the error message tells you.
org.springframework.data.mapping.model.MappingException:Could not lookup mapping metadata for domain class java.lang.Object!
Your application trys to lookup mapping for an Entity/Document of type Object
which is not definied.
I'm not very familiar to document store databases with spring but I think you have to annotate your id attribute in the Car
class with @Id
(At least thats the annotation for @Entity
classes. Maybe there is a different equivalent for @Document
classes.)
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.