Spring Boot + Hibernate Rest Api Controller获取状态404

时间:2016-10-16 18:01:51

标签: spring hibernate rest spring-mvc spring-boot

我正在使用spring boot和Hibernate创建一个简单的CRUD应用程序,当我尝试通过postman app访问我的api时遇到404错误,我经历了 Spring Boot: Cannot access REST Controller on localhost (404)以及404 not found while testing spring boot rest apiSpring boot + Hibernate他们都没有帮助。

控制器类是: -

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

}}

服务类是:

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

}

Dao类看起来像: -

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

}

和app.java类:

@ComponentScan({"spring","hibernate"})
@SpringBootApplication()
public class App 
{
public static void main( String[] args )
{
    SpringApplication.run(App.class, args);
}
}

包结构如下:[1]:https://i.stack.imgur.com/SBZ24.jpg

application.properties文件内容: -

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

在控制台服务器运行正常并且还创建了表但是在尝试访问api时遇到404错误

2 个答案:

答案 0 :(得分:1)

更改为

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

您需要注入LocalSessionFactoryBean而不是SessionFactory

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

然后使用它来获取会话

 Session session = getSessionFactory().openSession();

答案 1 :(得分:0)

在Application.properties文件中,我添加了

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

并在app类中添加了

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

这创建了会话工厂对象,我能够使用此Sessionfactory对象执行CRUD操作。