Всем привет.
Можете подсказать, что я делаю не правильно.
У меня есть класс расширяющий интерфейс JpaRepository. И что бы включить его я добавляю в класс приложения @EnableJpaRepositories("repositories").
Но выходит ошибка, вот такая -
Description:
Parameter 0 of constructor in com.idr.next.services.CellService required a bean of type 'com.idr.next.repositories.CellRepository' that could not be found.
Action:
Consider defining a bean of type 'com.idr.next.repositories.CellRepository' in your configuration.
Я порылся в интернете, нашел, что в @SpringBootApplication нужно передать папки.
@SpringBootApplication(scanBasePackages = ... ). Добавил, и о чудо, приложение выполнилось.
Но теперь когда я захожу на сайт "http://localhost:8080", выходит ошибка 404. То есть, походу сломались контроллеры.
Если кто знает как это исправить, можете подсказать?
Вот код приложения
package com.idr.next;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories("repositories.*")
@EntityScan(basePackages = "models.*")
public class NextApplication {
public static void main(String[] args) {
SpringApplication.run(NextApplication.class, args);
}
}
Модель
package com.idr.next.models;
import javax.persistence.*;
@Entity
@Table(name = "cells")
public class Cell {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "dayOfWeek")
private String dayOfWeek;
@Column(name = "time")
private String time;
@Column(name = "group")
private String group;
@Column(name = "subject")
private String subject;
@Column(name = "teacher")
private String teacher;
@Column(name = "room")
private int room;
public Cell(Long id, String dayOfWeek, String time, String group, String subject, String teacher, int room) {
this.id = id;
this.dayOfWeek = dayOfWeek;
this.time = time;
this.group = group;
this.subject = subject;
this.teacher = teacher;
this.room = room;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public int getRoom() {
return room;
}
public void setRoom(int room) {
this.room = room;
}
}
Это Контроллер
package com.idr.next.controllers;
import com.idr.next.models.Cell;
import com.idr.next.services.CellService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/schedule")
public class CellController {
private final Logger logger = LoggerFactory.getLogger(CellController.class);
private final CellService cellService;
public CellController(CellService cellService) {
this.cellService = cellService;
}
@GetMapping("/{group}")
public String cells(@PathVariable String group, Model model) {
model.addAttribute("cells", cellService.getCellsByGroup(group));
return "cells.html";
}
@GetMapping
public String cells(@RequestParam(required = false) Long id, Model model) {
model.addAttribute("cells", cellService.listCells());
model.addAttribute("del", cellService.getCellById(id));
return "cells.html";
}
@PostMapping("/create")
public String createCell(Cell cell) {
cellService.saveCell(cell);
return "redirect:/schedule";
}
@PostMapping("/delete/{id}")
public String deleteCell(@PathVariable Long id) {
cellService.deleteCell(id);
return "redirect:/schedule";
}
}
Сервис
package com.idr.next.services;
import com.idr.next.models.Cell;
import com.idr.next.repositories.CellRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CellService{
private final CellRepository cellRepository;
private static final Logger logger = LoggerFactory.getLogger(CellService.class);
public CellService(CellRepository cellRepository) {
this.cellRepository = cellRepository;
}
public List<Cell> listCells() {
return cellRepository.findAll();
}
public void saveCell(Cell cell) {
logger.info("Saving new {}", cell);
cellRepository.save(cell);
}
public void deleteCell(Long id) {
cellRepository.deleteById(id);
}
public Cell getCellById(Long id) {
return cellRepository.findById(id).orElse(null);
}
public List<Cell> getCellsByGroup(String group) {
return cellRepository.findByGroup(group);
}
}
И наконец репозиторий
package com.idr.next.repositories;
import com.idr.next.models.Cell;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CellRepository extends JpaRepository<Cell, Long> {
List<Cell> findByGroup(String group);
}