SpringBoot @ModelAttribute使用場景分析
前言
項(xiàng)目中遇到這么一個(gè)使用場景,用戶的登錄信息給予token保存,在需要有登錄信息的地方,每次都要去獲取用戶Id,但每次在請求方法中去獲取用戶信息,代碼重復(fù),冗余,很low于是想到了用@ModelAttribute
這個(gè)屬性
@ModelAttribute有三種用法:
- 可以標(biāo)注在方法上;
- 可以標(biāo)注在方法中的參數(shù)上;
- 還可以和@RequestMapping一起標(biāo)注在方法上;
使用場景
不用@ModelAttribute
時(shí)候在需要用戶信息的請求中每次需要單獨(dú)獲取用戶信息
String token = request.getAttribute("token").toString(); User LoginUser = tokenService.decodeToken(token);
代碼重復(fù)每次都需要單獨(dú)去寫,
于是我想到了去優(yōu)化一下代碼,在需要使用戶信息的controller
中寫一個(gè)公共方法,每次直接獲取就可以了
private User gerUserInfo(HttpServletRequest request){ String token = request.getAttribute("token").toString(); User LoginUser = tokenService.decodeToken(token); return LoginUser; }
這樣寫代碼是簡化了一些,但是沒什么特別大的改觀,還是要在每個(gè)需求用戶信息的請求Controller
中調(diào)用此方法取獲取用用戶信息,如果多個(gè)Controller
需要獲取用戶信息的話還需要重復(fù)寫
也是想到繼承,寫一個(gè)公共的controller
叫BaseController
,每次在需要用戶信息的controller中繼承BaseController
然后在調(diào)用就可以了
@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
中直接繼承調(diào)用就可以啦,但是并沒有根本解決我們的問題,我們還是需要寫重復(fù)代碼,在每個(gè)controller
單獨(dú)獲取用戶信息,這是最優(yōu)嘛?并不是?。?!
其實(shí)呢springboot
提供@ModelAttribute
這個(gè)注解屬性使用這個(gè)通過參數(shù)注入就可獲取啦
我們把上面的稍微調(diào)整一下如:
@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
中進(jìn)行參數(shù)映射就行啦
@ApiOperation(value = "用戶快過期優(yōu)惠卷信息",tags = "優(yōu)惠卷接口") @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
詳解
- 被
@ModelAttribute
注釋的方法會(huì)在此controller每個(gè)方法執(zhí)行前被執(zhí)行 - 標(biāo)注在方法上面的注解,將方法返回的對象存儲(chǔ)在model中,該方法在這個(gè)控制器其他映射方法執(zhí)行之前調(diào)用
@ModelAttribute
注釋一個(gè)方法的參數(shù) 從model中獲取參數(shù)@ModelAttribute("LoginUser") User user
參數(shù)user的值來源于BaseController
userInfo()方法中的model屬性
具體更詳細(xì)使用參考 @ModelAttribute注解的使用總結(jié)
到此這篇關(guān)于SpringBoot @ModelAttribute 用法的文章就介紹到這了,更多相關(guān)SpringBoot @ModelAttribute 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java動(dòng)態(tài)腳本Groovy獲取Bean技巧
這篇文章主要給大家分享的是Java動(dòng)態(tài)腳本Groovy獲取Bean技巧,在Java代碼中當(dāng)我們需要一個(gè)Bean對象,通常會(huì)使用spring中@Autowired注解,用來自動(dòng)裝配對象。下面我們一起進(jìn)入文章學(xué)習(xí)個(gè)表格多 詳細(xì)內(nèi)容吧2021-12-12使用BigDecimal進(jìn)行精確運(yùn)算(實(shí)現(xiàn)加減乘除運(yùn)算)
這篇文章主要介紹了如何使用BigDecimal進(jìn)行精確運(yùn)算,最后提供了一個(gè)工具類,該工具類提供加,減,乘,除運(yùn)算2013-11-11Java的ConcurrentHashMap中不能存儲(chǔ)null的原因解析
眾所周知,在Java中Map可以存儲(chǔ)null,而ConcurrentHashMap不能存儲(chǔ)null值,那么為什么呢?今天通過源碼分析給大家詳細(xì)解讀,感興趣的朋友一起看看吧2022-07-07