/** * 获取用户的session用户信息 */ @RequestMapping("/session") public R getCurrUser(HttpServletRequest request){ Long id = (Long)request.getSession().getAttribute("userId"); DefaultuserEntity user = defaultuserService.selectById(id); return R.ok().put("data", user); }
时间: 2024-04-27 09:22:58 浏览: 23
这是一个获取用户信息的接口,使用了Spring MVC框架中的@RequestMapping注解,用于接收HTTP请求并将其映射到Java方法上。方法中通过HttpServletRequest获取当前用户的Session,然后从Session中获取当前用户的id。接着,使用id查询出用户信息并返回给前端。该接口返回一个R对象,其中包含了查询到的用户信息。
相关问题
@RequestMapping("/session") public R getCurrUser(HttpServletRequest request){ Long id = (Long)request.getSession().getAttribute("userId"); UserEntity user = userService.selectById(id); return R.ok().put("data", user); }
这段代码是一个 Spring MVC 的控制器方法,用来获取当前用户的信息。它的请求路径是 "/session",通过 HttpServletRequest 对象获取当前用户的 Session 信息,从 Session 中获取用户的 ID,然后通过 UserService 的 selectById 方法查询数据库,获取用户的详细信息。最后将用户信息封装到 R.ok() 的返回结果中,并使用键值对 "data" 和 user 进行关联返回给前端。其中 R 是一个自定义的响应类,用于封装响应数据。
/** * 后端保存 */ @RequestMapping("/save") public R save(@RequestBody StoreupEntity storeup, HttpServletRequest request){ storeup.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); //ValidatorUtils.validateEntity(storeup); storeup.setUserid((Long)request.getSession().getAttribute("userId")); storeupService.insert(storeup); return R.ok(); }
这也是一个保存方法,使用了Spring框架的注解。该方法接受一个StoreupEntity类型的参数,该参数由前端发送过来的JSON数据转换而来。在方法中,首先为该实体生成一个唯一的ID,然后从当前用户的Session中获取用户ID,将其赋值给实体的userid属性。接着调用storeupService的insert方法将数据插入到数据库中。最后返回一个表示操作成功的R对象。与前一个方法的区别在于前一个方法是前端保存,而该方法是后端保存。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)