Search code examples
springspring-bootspring-mvcspring-data-jpa

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback with no error in console


Title: Spring Boot Application Issue: [Briefly Describe the Problem] Description: I’m working on a Spring Boot application with a JPA repository and am encountering an issue. My application starts without errors, but I am facing [describe the specific issue, e.g., "problems retrieving tasks from the database" or "an unexpected exception"]. Here’s the relevant code and configuration:

Code: Spring Boot Application:

package com.example.ToDoApplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.repository")
public class ToDoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ToDoApplication.class, args);
    }
}

Entity Class:

package com.example.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Tasks {
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Id
    private Long id;
    private String title;
    private String description;
    @Column(name = "is_completed")
    private boolean completed;

    // Getters and Setters
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public boolean isCompleted() {
        return completed;
    }
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
}

Controller

package com.example.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.entity.Tasks;
import com.example.service.TaskService;

@RestController
@RequestMapping("/tasks")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @GetMapping("/hello")
    public List<Tasks> getAllTasks() {
        return taskService.getAllTasks();
    }

    @GetMapping("/{id}")
    public Tasks getTaskById(@PathVariable Long id) {
        return taskService.getById(id).orElse(null);
    }

    @PostMapping
    public Tasks createTask(@RequestBody Tasks task) {
        return taskService.saveTask(task);
    }

    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Long id) {
        taskService.deleteTask(id);
        return "Deleted Successfully";
    }
}

Service:

package com.example.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entity.Tasks;
import com.example.repository.TaskRepository;

@Service
public class TaskService {
    private static final Logger logger = LoggerFactory.getLogger(TaskService.class);
    @Autowired
    private TaskRepository taskRepository;

    public List<Tasks> getAllTasks() {
        logger.info("Fetching all tasks from the database...");
        List<Tasks> tasks = taskRepository.findAll();
        logger.info("Number of tasks retrieved: " + tasks.size());
        return tasks;
    }

    public Optional<Tasks> getById(Long id) {
        return taskRepository.findById(id);
    }

    public Tasks saveTask(Tasks task) {
        return taskRepository.save(task);
    }

    public void deleteTask(Long id) {
        taskRepository.deleteById(id);
    }
}

Repository

package com.example.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.Tasks;

@Repository
public interface TaskRepository extends JpaRepository<Tasks, Long> {
}

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/demo_task
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root123
spring.jpa.hibernate.ddl-auto=update
server.port=8091


Solution

  • The package statement(first line) in your ToDoApplication class is wrong.

    It should be

    package com.example;

    instead of

    package com.example.ToDoApplication;

    You don't need @EnableJpaRepositories annotation unless you have different sets of Repositories/Repository configurations(which means different databases in the same application), you don't need to specify.

    By default, if Spring Boot Auto-Configuration detects Spring Data JPA on the class path interfaces with @Repository that extends @JpaRepository or @CrudRepository are considered as a JPA Repository.