为什么邮递员表示找不到 404

问题描述

我正在尝试从数据库返回数据但发生了此错误我不知道错误是 404 这是我的代码http://localhost:8085/exercices

我的运动课


import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "exercice")
public class exercices implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idExercice;

    private String nomExercice;
    private String LinkExercice;
    @ManyToOne
    private patient patient;

    public exercices(Long idExercice,String nomExercice,String linkExercice) {
        this.idExercice = idExercice;
        this.nomExercice = nomExercice;
        this.LinkExercice = linkExercice;
        this.patient = new patient();
    }

    public exercices(String nomExercice,String linkExercice) {
        this.nomExercice = nomExercice;
        LinkExercice = linkExercice;
    }

    public Long getIdExercice() {
        return idExercice;
    }

    public void setIdExercice(Long idExercice) {
        this.idExercice = idExercice;
    }

    public String getNomExercice() {
        return nomExercice;
    }

    public void setNomExercice(String nomExercice) {
        this.nomExercice = nomExercice;
    }

    public String getLinkExercice() {
        return LinkExercice;
    }

    public void setLinkExercice(String linkExercice) {
        LinkExercice = linkExercice;
    }

    @Override
    public String toString() {
        return "exercices [LinkExercice=" + LinkExercice + ",idExercice=" + idExercice + ",nomExercice=" + nomExercice
                + "]";
    }

}

运动控制器


import java.util.List;

import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import healthP.Health.Progress.model.exercices;
import healthP.Health.Progress.service.ExerciceService;

@RestController
@RequestMapping()
public class ExerciceController {
    @Autowired
    private final ExerciceService exserv;

    public ExerciceController(ExerciceService exserv) {
        this.exserv = exserv;
    }

    @GetMapping("/exercices")
    public ResponseEntity<List<exercices>> getAllExercices() {
        List<exercices> ex = this.exserv.findAllExercices();
        return new ResponseEntity<>(ex,HttpStatus.OK);

    }

}

运动服务


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import healthP.Health.Progress.model.exercices;
import healthP.Health.Progress.repo.ExercicesRepo;

@Service
public class ExerciceService {
    private final ExercicesRepo ExRepo;

    @Autowired
    public ExerciceService(ExercicesRepo ExRepo) {
        this.ExRepo = ExRepo;
    }

    public List<exercices> findAllExercices() {
        return this.ExRepo.findAll();
    }

}

界面


import org.springframework.data.jpa.repository.JpaRepository;

import healthP.Health.Progress.model.exercices;

public interface ExercicesRepo extends JpaRepository<exercices,Long> {

}

这是耐心班,因为我还想检查两个班级之间的关系是否在练习班的构造者中是正确的和空间的

package healthP.Health.Progress.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import javax.persistence.Table;
//import org.hibernate.annotations.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity
@Table(name = "patient")
// @Table(name = patient.TABLE_NAME)
public class patient implements Serializable {
    // public static final String TABLE_NAME="PATIENT";
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int score;
    @Column(nullable = false,updatable = false)
    private Long id;
    private String name;
    private String email;
    private String password;
    @Column(nullable = false,updatable = false)
    private String codePatient;

    public patient() {
    }

    public patient(String codep,String name,String email,String password) {
        this.codePatient = codep;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getpassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCodePatient() {
        return this.codePatient;
    }

    public void setCodePatient(String codePatient) {
        this.codePatient = codePatient;
    }

    public void setscore(int score) {
        this.score = score;
    }

    public int getscore() {
        return this.score;
    }

    @Override
    public String toString() {
        return "{" + " id='" + getId() + "'" + ",name='" + getName() + "'" + ",email='" + getEmail() + "'"
                + ",password='" + getpassword() + "'" + "}";
    }
}```

解决方法

将 api-path 添加到 ExerciceController 的 requestmapping 中,就变成了

@RequestMapping("/api")
public class ExerciceController {

编辑:

您需要为练习实体添加默认构造函数

public exercices(){}

现在您可以作为 http://localhost:8085/api/exercices

访问它

PS:与问题无关,对实体 excercise 使用单数类名而不是 excercises

,

您可以对代码进行少量更改并遵循以下任一方法:

1- 删除@RequestMapping()

您可以访问端点:http://localhost:8085/exercices

2- 如果您想管理 api 版本控制,请使用 @RequestMapping("/api/v1")

您可以使用端点:http://localhost:8085/api/v1/exercices