Search code examples
javaspring-bootfindall

Why postman indicated 404 not found


I'm trying to return data from database but this error has occurred I don't know the error is 404 this is my code : http://localhost:8085/exercices

my Exercice class


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

}

ExerciceController


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

    }

}

ExerciceService


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

}

interface


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

import healthP.Health.Progress.model.exercices;

public interface ExercicesRepo extends JpaRepository<exercices, Long> {

}

and this is patient class because I want also check if the relation between two classes are correcte and spacially in the constructeur of exercice class.

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() + "'" + "}";
    }
}```

Solution

  • add api-path to requestmapping of ExerciceController , so it becomes

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

    EDIT:

    You need to add default constructor to excercises entity

    public exercices(){}
    

    now you can access it as http://localhost:8085/api/exercices

    PS: unrelated to question, use singular class name for entity excercise instead of excercises