Springboot HttpSession缺少首要或唯一的构造函数

报错java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpSession

img

学springboot的时候写个登录功能时遇到的。网上没有找到相关解决方案,类似的问题全是报错list、request的,没有session的报错解答

,HttpSession是一个接口,不能直接实例化,需要通过HttpServletRequest对象的getSession()方法来获取一个HttpSession对象。因此,解决该问题的方法就是,在使用HttpSession之前,需要先通过HttpServletRequest对象的getSession()方法获取HttpSession对象。

这个错误是由于HttpSession是接口,Spring无法找到唯一的构造函数来实例化该对象。因此,需要使用其他方式来注入HttpSession对象。

一种解决方案是使用@Autowired注释进行属性注入,另一种解决方案是使用@Resource注释进行属性注入