You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用问题:无/error映射返回默认白标错误页

Spring Boot集成JPA Repository接口访问白标错误问题

应用启动无报错,但访问接口时出现白标错误页(Whitelabel Error Page),提示该应用未为/error路径配置显式映射,控制台无任何错误输出。

相关代码与配置

Spring Boot应用类

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);
    }
}

实体类

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;
    }
}

控制器类

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";
    }
}

服务类

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);
    }
}

仓库接口

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

问题原因

核心问题是Spring未扫描到控制器类:

  • @SpringBootApplication注解默认只扫描当前类所在包及其子包,应用类位于com.example.ToDoApplication包,而控制器、服务、实体等类位于com.example.controller、com.example.service等同级包,不在默认扫描范围内,导致控制器未被注册,接口映射不存在,访问时触发白标错误。

解决方案

有两种可行的修复方式:

方式一:指定组件扫描包

修改@SpringBootApplication注解,通过scanBasePackages指定扫描根包com.example,覆盖所有子包:

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

方式二:调整应用类包路径

将应用类移动到com.example包下,这样默认扫描规则会自动覆盖所有子包(controller、service、repository等):

package com.example;

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);
    }
}

修复后重启应用,访问正确的接口路径(例如GET http://localhost:8091/tasks/hello)即可正常响应。


内容的提问来源于stack exchange,提问作者Raji S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:42:02