SpringBoot @ModelAttribute 用法


每日一句
世界没有悲剧和喜剧之分,如果你能从悲剧中走出来,那就是喜剧;如果你沉缅于喜剧之中,那它就是悲剧。by 上德若谷

前言

项目中遇到这么一个使用场景,用户的登录信息给予token保存,在需要有登录信息的地方,每次都要去获取用户Id,但每次在请求方法中去获取用户信息,代码重复,冗余,很low于是想到了用@ModelAttribute 这个属性

使用场景

不用@ModelAttribute 时候在需要用户信息的请求中每次需要单独获取用户信息

  String token = request.getAttribute("token").toString();
User LoginUser = tokenService.decodeToken(token);

代码重复每次都需要单独去写,

于是我想到了去优化一下代码,在需要使用户信息的controller中写一个公共方法,每次直接获取就可以了

private User gerUserInfo(HttpServletRequest request){
String token = request.getAttribute("token").toString();
User LoginUser = tokenService.decodeToken(token);
return LoginUser;
}

这样写代码是简化了一些,但是没什么特别大的改观,还是要在每个需求用户信息的请求Controller中调用此方法取获取用用户信息,如果多个Controller需要获取用户信息的话还需要重复写

也是想到继承,写一个公共的controllerBaseController,每次在需要用户信息的controller中继承BaseController 然后在调用就可以了

@RestController
public class BaseController {
@Autowired
private TokenService tokenService;


private User gerUserInfo(HttpServletRequest request){
String token = request.getAttribute("token").toString();
User LoginUser = tokenService.decodeToken(token);
return LoginUser;
}
}

这样看上去似乎比之前两种做法都简单便捷很多,在需要使用用户信息的controller中直接继承调用就可以啦,但是并没有根本解决我们的问题,我们还是需要写重复代码,在每个controller单独获取用户信息,这是最优嘛?并不是!!!

其实呢springboot提供@ModelAttribute这个注解属性使用这个通过参数注入就可获取啦

我们把上面的稍微调整一下如:

@RestController
public class BaseController {
@Autowired
private TokenService tokenService;


@ModelAttribute
public void userInfo(ModelMap modelMap, HttpServletRequest request) {
String token = request.getAttribute("token").toString();
User LoginUser = tokenService.decodeToken(token);

modelMap.addAttribute("LoginUser", LoginUser);
modelMap.addAttribute("userId", LoginUser.getUserId());

}
}

然后在需要使用用户信息的controller中进行参数映射就行啦

@ApiOperation(value = "用户快过期优惠卷信息",tags = "优惠卷接口")
@GetMapping("/expiredCoupon")
public List<Coupon> userExpiredCoupon(@ModelAttribute("userId") @ApiParam(hidden = true) String userId){
return couponService.getUserExpiredCoupon(userId);
}
@GetMapping("/info")
@ApiOperation("获取用户信息")
public User getUseInfo(@ModelAttribute("LoginUser") User user) {
return user;
}

这样用户信息通过形参直接注入到controller中,我们直接在请求中使用就可以啦

@ModelAttribute详解

  1. @ModelAttribute注释的方法会在此controller每个方法执行前被执行 标注在方法上面的注解,将方法返回的对象存储在model中,该方法在这个控制器其他映射方法执行之前调用
  2. @ModelAttribute注释一个方法的参数  从model中获取参数@ModelAttribute("LoginUser") User user参数user的值来源于BaseControlleruserInfo()方法中的model属性

具体更详细使用参考 @ModelAttribute注解的使用总结[1]

参考资料

[1]

@ModelAttribute注解的使用总结: https://blog.csdn.net/leo3070/article/details/81046383

SpringBoot  @ModelAttribute 用法

SpringBoot  @ModelAttribute 用法


原文始发于微信公众号(猿小叔):SpringBoot @ModelAttribute 用法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/27993.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!