1

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

2 Answers 2

1

Change to

@ComponentScan({"com.student.studentdb"})

And you need inject LocalSessionFactoryBean instead of SessionFactory

   @Autowired
    @Qualifier("sessionFactory")
    private LocalSessionFactoryBean sessionFactory;

then use that to get session

 Session session = getSessionFactory().openSession();
Sign up to request clarification or add additional context in comments.

6 Comments

when i add com.student.studentdb and change to @ComponentScan({"spring","hibernate","com.student.studentdb"})
i got the exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
how about adding @EnableAutoConfiguration to the Application class
what is the url did you try @shashank
|
0

In the Application.properties file i added

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

and in the app class i added

@Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }

This created the session factory object and i was able to perform CRUD operation with this Sessionfactory object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.