[英]Spring Boot+Hibernate Rest Api Controller getting status 404
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.我正在按照使用 spring boot 和 Hibernate 的一些教程创建一个简单的 CRUD 应用程序,尝试通过邮递员应用程序访问我的 api 时出现 404 错误,我经历了Spring Boot:无法访问本地主机上的 REST 控制器(404)和404 未找到在测试 spring boot rest api和Spring boot + Hibernate 时,它们都没有帮助。
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:- 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);
}
and the app.java class:和 app.java 类:
@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.stack.imgur.com/SBZ24.jpg包结构如下:[1]: https : //i.stack.imgur.com/SBZ24.jpg
The application.properties file content:- 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
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在控制台服务器运行良好,表也已创建,但在尝试访问 api 时出现 404 错误
Change to改成
@ComponentScan({"com.student.studentdb"})
And you need inject LocalSessionFactoryBean instead of SessionFactory你需要注入 LocalSessionFactoryBean 而不是 SessionFactory
@Autowired
@Qualifier("sessionFactory")
private LocalSessionFactoryBean sessionFactory;
then use that to get session然后用它来获取会话
Session session = getSessionFactory().openSession();
In the Application.properties file i added在 Application.properties 文件中,我添加了
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.这创建了会话工厂对象,我能够使用这个 Sessionfactory 对象执行 CRUD 操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.