Akkië
Akkië

Reputation: 23

Spring Boot Hibernate CRUD REST API 404 Error

I am simply trying to create a Spring boot Hibernate CRUD REST API through this code:

EmployeController.java

@RestController
@RequestMapping("/api")

    public class EmployeController {

        @Autowired
        private EmployeService employeService;

        @GetMapping("/employe")
        public List<Employe> get(){
            return employeService.get();
        }
    }

Employe.java

@Entity
@Table(name="employe")
public class Employe {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String gender;
    @Column
    private String department;
    @Column
    private Date dob;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Date getDob() {
        return dob;
    }
    public void setDob(Date dob) {
        this.dob = dob;
    }
    @Override
    public String toString() {
        return "Employe [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
                + dob + "]";
    }


}

EmployeService.java

public interface EmployeService {
    List<Employe> get();
    Employe get(int id);
    void save(Employe employe);
    void delete(int id);
}

EmployeServiceImplement.java

@Service
public class EmployeServiceImplement implements EmployeService {

    @Autowired
    private EmployeDAO employeDAO;

    @Transactional
    @Override
    public List<Employe> get() {
        return employeDAO.get();
    }
}

EmployeDAO.java

public interface EmployeDAO {
    List<Employe> get();
    Employe get(int id);
    void save(Employe employe);
    void delete(int id);
}

EmployeDAOImplement.java

@Repository
public class EmployeDAOImplement implements EmployeDAO {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Employe> get() {
        Session currentSession = entityManager.unwrap(Session.class);
        Query<Employe> query = currentSession.createQuery("from Employe", Employe.class);
        List<Employe>list = query.getResultList();  
        return list;
    }
}

I have write all the configuration related to MySQl database into the application.properties and when i run this project as Spring Boot App and go to the Postman and tried like this enter image description here

and i a unable to understan why it always throws 404 error every time , can anyone tell me what i am missing in this code.

Upvotes: 0

Views: 507

Answers (3)

randy
randy

Reputation: 26

Try with this GET request, it may help you:

http://localhost:8080/api

Upvotes: 1

Moeen Tabrizi
Moeen Tabrizi

Reputation: 44

I checked your code. where is @RestController for your Controller file and where is @RequestMapping For your method in Controller class?

maybe you should write something like this according to your need. tell me if you need more help.

@RestController
@RequestMapping("/api")
public class EmployeController {

   @RequestMapping(value = "/employ")
   public void employ()  {
   }
}

Upvotes: 1

abnormalThinker
abnormalThinker

Reputation: 3

Instead of this -

@Override

public List get()

Use this -

@RequestMapping(value = "/Employe", method = RequestMethod.GET)

public List get()

Upvotes: 0

Related Questions