【问题标题】:Spring Boot+Hibernate Rest Api Controller getting status 404Spring Boot+Hibernate Rest Api Controller 获取状态 4​​04
【发布时间】:2017-02-25 16:22:12
【问题描述】:

我正在按照一些使用 Spring Boot 和 Hibernate 的教程创建一个简单的 CRUD 应用程序,尝试通过邮递员应用程序访问我的 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 错误

【问题讨论】:

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


    【解决方案1】:

    改成

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

    你需要注入 LocalSessionFactoryBean 而不是 SessionFactory

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

    然后用它来获取会话

     Session session = getSessionFactory().openSession();
    

    【讨论】:

    • 当我添加 com.student.studentdb 并更改为 @ComponentScan({"spring","hibernate","com.student.studentdb"})
    • 我遇到了异常
    • org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到[org.hibernate.SessionFactory]类型的合格bean:预计至少有1个bean有资格作为此依赖的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    • 在Application类中添加@EnableAutoConfiguration怎么样
    • 你尝试@shashank的网址是什么
    【解决方案2】:

    在我添加的 Application.properties 文件中

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

    在我添加的应用类中

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

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

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2020-05-03
      • 2023-03-17
      • 2020-10-29
      相关资源
      最近更新 更多