Reputation: 616
I am trying to write a simple Spring app (with Maven) that would connect to a mLab mongoDB database, but when I try to run it an error comes up that says:
*************************** APPLICATION FAILED TO START
Description:
Field carService in main.Controller.CarController required a bean of type 'main.Service.CarService' that could not be found.
Action:
Consider defining a bean of type 'main.Service.CarService' in your configuration.
I tried some of the methods that I found on StackOverflow (for example reorganising the project structure) but that didn't help, or I'm missing something. This is my project structure:
And my project files:
package main.Controller;
import main.Entity.Car;
import main.Service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
@RestController
@RequestMapping("/cars")
public class CarController {
@Autowired
private CarService carService;
@RequestMapping(method = RequestMethod.GET)
public Collection<Car> getAllCars(){
return this.carService.getAllCars();
}
}
server.port = 3000
spring.data.mongodb.uri=mongodb://user:[email protected]:11309/cars
package main.Controller;
import main.Entity.Car;
import main.Service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
@RestController
@RequestMapping("/cars")
public class CarController {
@Autowired
private CarService carService;
@RequestMapping(method = RequestMethod.GET)
public Collection<Car> getAllCars(){
return this.carService.getAllCars();
}
}
package main.Service;
import main.Dao.CarRepository;
import main.Entity.Car;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
public class CarService {
@Autowired
private CarRepository carRepo;
public Collection<Car> getAllCars() {
return carRepo.findAll();
}
}
package main.Dao;
import main.Entity.Car;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface CarRepository extends MongoRepository<Car, String> {
public Car findByModel(String model);
public List<Car> findByMake(String make);
}
package main.Entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
@Document(collection = "cars")
public class Car {
@Id
public String id;
public String make;
public String model;
public String body;
public String engine;
public int totalRentals;
public int seats;
public int price;
public double totalIncome;
public Date updated_date;
public Car(String id, String make, String model, String body, String engine, int totalRentals, int seats, int price, double totalIncome, Date updated_date) {
this.id = id;
this.make = make;
this.model = model;
this.body = body;
this.engine = engine;
this.totalRentals = totalRentals;
this.seats = seats;
this.price = price;
this.totalIncome = totalIncome;
this.updated_date = updated_date;
}
@Override
public String toString() {
return String.format(
"Car[id=%s, make='%s', model='%s', body='%s', engine='%s', totalRentals='%s', seats='%s', price='%s', totalIncome='%s', updated_date='%s']",
id, make, model, body, engine, totalRentals, seats, price, totalIncome, updated_date);
}
}
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.gdyniarent</groupId>
<artifactId>GdyniaRent backend API</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
Upvotes: 0
Views: 521
Reputation: 18235
public class CarService {
is just a plain pojo.
Mark it as a bean:
@Service
public class CarService {
Upvotes: 4