繁体   English   中英

Spring Boot + Hibernate Rest Api Controller 获取状态 4​​04

[英]Spring Boot+Hibernate Rest Api Controller getting status 404

我正在按照使用 spring boot 和 Hibernate 的一些教程创建一个简单的 CRUD 应用程序,尝试通过邮递员应用程序访问我的 api 时出现 404 错误,我经历了Spring Boot:无法访问本地主机上的 REST 控制器(404)404 未找到在测试 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 错误

改成

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

你需要注入 LocalSessionFactoryBean 而不是 SessionFactory

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

然后用它来获取会话

 Session session = getSessionFactory().openSession();

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

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

在我添加的应用程序类中

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM