SpringBoot獲取Request和Response方法代碼解析
通過靜態(tài)方法獲取,你也可以封裝一個(gè)靜態(tài)方法出來
@GetMapping(value = '')public String center() { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse(); //...}
通過參數(shù)直接獲取,只要在你的方法上加上參數(shù),Springboot就會(huì)幫你綁定,你可以直接使用。如果你的方法有其他參數(shù),把這兩個(gè)加到后面即可。
@GetMapping(value = '')public String center(HttpServletRequest request,HttpServletResponse response) { //...}
注入到類,這樣就不用每個(gè)方法都寫了
@Autowiredprivate HttpServletRequest request;@Autowiredprivate HttpServletResponse response;@GetMapping(value = '')public String center() { //...}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android打包篇:Android Studio將代碼打包成jar包教程2. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis3. SpringBoot+TestNG單元測試的實(shí)現(xiàn)4. Springboot 全局日期格式化處理的實(shí)現(xiàn)5. vue實(shí)現(xiàn)web在線聊天功能6. 解決Android Studio 格式化 Format代碼快捷鍵問題7. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題8. JavaScript實(shí)現(xiàn)頁面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例9. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼10. Java使用Tesseract-Ocr識(shí)別數(shù)字
