Spring Boot REST API POST请求返回404/405错误,所有接口无法正常路由求助
Spring Boot REST API POST请求返回404/405错误,所有接口无法正常路由求助
大家好,我现在用Spring Boot开发一个任务管理的REST API,遇到了一个棘手的问题:所有接口都无法正常路由,发送POST请求到/tasks时返回404 Method Not Allowed,其他GET/PUT/DELETE请求也都是404或者405错误。我期望POST /tasks能映射到控制器里的createTask()方法,但完全没生效,想请大家帮忙排查下问题。
问题详情
- 发送
POST http://localhost:8080/tasks返回404错误 - 尝试带上应用名前缀的
http://localhost:8080/todomanager/tasks同样返回404 - 其他接口比如GET
/tasks也无法访问,返回404
我已经做过的排查操作:
- 确认Postman的请求URL、HTTP方法完全正确
- 给
TaskController类添加了@RequestMapping("/tasks")类级别映射 - 控制台能输出
taskcontroller init,说明控制器已成功初始化 - 多次重启应用,问题依然存在
相关代码
主启动类
@SpringBootApplication public class ToDoManagerApplication { public static void main(String[] args) { SpringApplication.run(ToDoManagerApplication.class, args); } }
TaskController控制器
package az.edu.turing.todomanager.controller; import az.edu.turing.todomanager.model.Task; import az.edu.turing.todomanager.service.TaskService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/tasks") // 后续新增的类级别路由映射 public class TaskController { private final TaskService taskService; public TaskController(TaskService taskService) { this.taskService = taskService; System.out.println("taskcontroller init"); } @GetMapping public List<Task> getAllTasks(){ return taskService.getAllTasks(); } @GetMapping("/{id}") public ResponseEntity<Task> getTaskById(@PathVariable Long id) { return taskService.getTaskById(id) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @PostMapping public Task createTask(@RequestBody Task task){ return taskService.createTask(task); } @PutMapping("/{id}") public ResponseEntity<Task> updateTask(@PathVariable Long id, @RequestBody Task updatedTask) { try { Task task = taskService.updateTask(id, updatedTask); return ResponseEntity.ok(task); } catch (RuntimeException e) { return ResponseEntity.notFound().build(); } } @DeleteMapping("/{id}") public ResponseEntity<Void> deleteTask(@PathVariable Long id) { taskService.deleteTask(id); return ResponseEntity.noContent().build(); } }
TaskServiceImpl业务实现类
package az.edu.turing.todomanager.service.impl; import az.edu.turing.todomanager.model.Task; import az.edu.turing.todomanager.repository.TaskRepository; import az.edu.turing.todomanager.service.TaskService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service @RequiredArgsConstructor public class TaskServiceImpl implements TaskService{ private final TaskRepository taskRepository; @Override public List<Task> getAllTasks() { return taskRepository.findAll(); } @Override public Task createTask(Task task) { return taskRepository.save(task); } @Override public Optional<Task> getTaskById(long taskId) { return taskRepository.findById(taskId); } @Override public Task updateTask(Long taskId, Task updatedTask) { return taskRepository.findById(taskId) .map(existingTask -> { existingTask.setName(updatedTask.getName()); existingTask.setDueDate(updatedTask.getDueDate()); existingTask.setStatus(updatedTask.getStatus()); return taskRepository.save(existingTask); }) .orElseThrow(() -> new RuntimeException("Task not found with id: " + taskId)); } @Override public void deleteTask(Long taskId) { taskRepository.deleteById(taskId); } }
application.properties配置
spring.application.name=to-do-manager spring.datasource.url=jdbc:postgresql://localhost:5432/todomanager spring.datasource.username=postgres spring.datasource.password=mypassword spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect management.endpoints.web.exposure.include=*
有没有朋友能帮我定位下问题?卡在这里好久了,实在找不到头绪😭
内容来源于stack exchange




