I am creating a simple CRUD application following some tutorial using spring boot and Hibernate,I am getting 404 error when trying to access my api's through postman app,i went through Spring Boot: Cannot access REST Controller on localhost (404) and 404 not found while testing spring boot rest api and Spring boot + Hibernate none of them helped.
Controller Class is:-
@RestController @RequestMapping(value="/students/") public class studentController { @Autowired private StudentService service; @RequestMapping(value="getstudent",method=RequestMethod.GET) public Collection<Student> getStudent(){ return service.getStudent(); } @RequestMapping(value="getstudent/{id}",method=RequestMethod.GET) public Student getStudentById(@PathVariable("id") Integer id){ return service.getStudentById(id); } @RequestMapping(value="getstudent/{id}",method=RequestMethod.DELETE) public void deleteStudentById(@PathVariable("id") Integer id){ service.deleteStudentById(id); } @RequestMapping(value="updatestudent",method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE) public void updateStudentById(@RequestBody Student student) { service.updateStudent(student); } @RequestMapping(value="createstudent",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE) public void createStudent(@RequestBody Student student){ service.addStudent(student); }} The service class is:
@Service @Qualifier("mysql") public class StudentService { @Autowired private StudentDaoInt dao; @Transactional public Collection<Student> getStudent(){ return dao.getStudent(); } @Transactional public Student getStudentById(Integer id){ return dao.getStudentById(id); } @Transactional public void deleteStudentById(Integer id) { dao.deleteStudentById(id); } @Transactional public void updateStudent(Student student) { dao.updateStudent(student); } @Transactional public void addStudent(Student student) { dao.addStudent(student); } The Dao class looks like:-
@Repository @Qualifier("mysql") public class StudentDaoMySql implements StudentDaoInt{ @Autowired private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sf){ this.sessionFactory = sf; } @Override public Collection<Student> getStudent() { return sessionFactory.getCurrentSession().createQuery("from Student").list(); } @Override public Student getStudentById(Integer id) { return (Student) sessionFactory.getCurrentSession().createQuery("from Student s wehre s.id=id").list(); } @Override public void deleteStudentById(Integer id) { sessionFactory.getCurrentSession().createQuery("DELETE from Student s wehre s.id=id").executeUpdate(); } @Override public void updateStudent(Student student) { Query q=sessionFactory.getCurrentSession().createQuery("update Student set name=:myname,age=:myage where id=:myid"); q.setParameter("myname", student.getName()); q.setParameter("myage", student.getAge()); q.setParameter("myid", student.getId()); q.executeUpdate(); } @Override public void addStudent(Student student) { sessionFactory.getCurrentSession().save(student); } and the app.java class:
@ComponentScan({"spring","hibernate"}) @SpringBootApplication() public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } } The package structure looks like:[1]: https://i.sstatic.net/SBZ24.jpg
The application.properties file content:-
spring.datasource.url = jdbc:mysql://localhost:3306/TestRest spring.datasource.username = root spring.datasource.password = dinga spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect In the console server is running fine and table is also created but i am getting 404 error when trying to access the api's