spring 无法删除数据:出现意外错误(类型=找不到,状态=404)

gywdnpxw  于 2023-03-28  发布在  Spring
关注(0)|答案(1)|浏览(187)

我是新来的尝试了可能重复的解决方案,但无法解决它。我试图建立一个非常简单的医院管理系统。我可以创建患者数据,但当我点击删除到红色按钮时,它给我这个错误。
view database
患者控制器:

package com.laboratory.app.controller;
import com.laboratory.app.entity.Patient;
import com.laboratory.app.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class PatientController {

    // Patient service object
    @Autowired
    private PatientService patientService;

    @GetMapping(value="/register_patient")
    public ModelAndView registerPatient(Model model) {
        ModelAndView registerView = new ModelAndView("registerPatient/registerPatient");
        Patient newPatient = new Patient();
        model.addAttribute("patient", newPatient);
        return registerView;
    }

    @PostMapping(value="/save")
    public ModelAndView savePatient(@ModelAttribute("patient") Patient newPatient) {
        ModelAndView view = new ModelAndView("registerPatient/registeredSuccessfully");
        patientService.savePatient(newPatient);
        return view;
    }

    @DeleteMapping(value="/delete")
    public ModelAndView deletePatient(@RequestParam("patientId") String id) {
        ModelAndView view = new ModelAndView("home");
        patientService.deletePatient(Long.valueOf(id));
        return view;
    }

    @GetMapping(value="/view_database")
    public ModelAndView viewDatabase(Model model) {
        List<Patient> theEmployees = patientService.listAllPatients();
        model.addAttribute("patients", theEmployees);
        ModelAndView view = new ModelAndView("viewDatabase");
        return view;
    }

}

患者资料库:

package com.laboratory.app.repository;
import com.laboratory.app.entity.Patient;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PatientRepository extends JpaRepository<Patient, Long> {

    List<Patient> findAll(); // lists all the patients

    List<Patient> findAllByOrderByDateAddedAsc(); // show data by newest to oldest

    List<Patient> findAllByOrderByDateAddedDesc(); // show data by oldest to newest

    void deleteById(Long id);

    long count(); // return how many patients are there

    Optional<Patient> findById(Long id); // return the @Entity by id

    Patient save(Patient patient); // saves the patient into the database

    // show related data regarding searched name (case)
    List<Patient> findAllByPatientNameContainingIgnoreCase(String name);
    List<Patient> findAllByPatientSurnameContainingIgnoreCase(String name);

}

患者服务:

package com.laboratory.app.service;
import com.laboratory.app.entity.Patient;
import com.laboratory.app.repository.PatientRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class PatientService {
    @Autowired
    PatientRepository patientRepository;

    public List<Patient> listAllPatients() {
        return patientRepository.findAll();
    }

    public Patient savePatient(Patient patient) {
        return  patientRepository.save(patient);
    }

    public void deletePatient(Long id) {
        patientRepository.deleteById(id);
    }

    public long getPatientNum() {
        return patientRepository.count();
    }

    public Optional<Patient> findPatientById(Long id) {
        return patientRepository.findById(id);
    }

    public List<Patient> findPatientByName(String name) {
        return patientRepository.findAllByPatientNameContainingIgnoreCase(name);
    }

    public List<Patient> findPatientBySurname(String surname) {
        return patientRepository.findAllByPatientSurnameContainingIgnoreCase(surname);
    }

    public List<Patient> getAllPatientsSortedByDateAdded(boolean ascending) {
        if (ascending) {
            return patientRepository.findAllByOrderByDateAddedAsc();
        } else {
            return patientRepository.findAllByOrderByDateAddedDesc();
        }
    }
}

这是HTML页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" href="/style/viewDatabase.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <meta charset="UTF-8">
    <title>Patient Database</title>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand text-white mr-3" href="/">Home</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active text-white" aria-current="page" href="/home#about-id">About</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-white" href="view_database">Database</a>
                    </li>
                </ul>
                <div>
                    <a class="nav-link text-white" href="register_patient">Register Data</a>
                </div>
                <div>
                    <a class="nav-link text-white" href="log_in">Log In</a>
                </div>
                <div>
                    <a class="nav-link text-white" href="sign_up">Sign Up</a>
                </div>
            </div>
        </div>
    </nav>
    <div class="container">
        <h3 class="mt-4">This is the database page</h3>
        <p class="mb-3">You can see the table below.</p>
        <table class="table table-striped table-hover">
            <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">First</th>
                <th scope="col">Last</th>
                <th scope="col">Diagnosis</th>
                <th scope="col">For More</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="patient : ${patients}">
                <td th:text="${patient.patientId}"></td>
                <td th:text="${patient.patientName}"></td>
                <td th:text="${patient.patientSurname}"></td>
                <td th:text="${patient.patientDisease}"></td>
                <td>
                    <div class="show-more">
                        <strong><a href="#">Show</a></strong>
                    </div>
                </td>
                <td>
                    <a th:href="@{/patients/delete(patientId=${patient.getPatientId()})}"
                       onclick="if (!(confirm('Are you sure you want to delete this patient? ' +
                        'You can\'t take this back.'))) return false"
                       class="btn btn-danger btn-sm text-white"
                    >
                        Delete
                    </a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

错在哪里?
这是一个简单的REST API。我尝试了@DeleteMapping的各种组合,如@GetMapping@RequestMapping。此外,当我检查mysql终端时,它不会删除所选的患者。
谢谢您。

5sxhfpxr

5sxhfpxr1#

一个404总是因为你的thymeleave页面中的路径,即/patients/delete(patientId=${patient.getPatientId()})在你的控制器中找不到。你的控制器只包含/delete/register_patient/save/view_database。我可以看到http://localhost:3000/view_database工作正常,所以它使用正确的路径。将/patients/delete更改为/delete
404对@Entity@Repository代码没有任何作用。
至于使用@DeleteMethod,你不能因为这个原因。你会得到一个405(方法不允许)。尝试@PostMethod
在尝试从thymeleaf/html调用之前,请使用Postman确认您的URL/路径

相关问题