SpringBoot獲取Request和Response方法代碼解析
通過靜態(tài)方法獲取,你也可以封裝一個靜態(tài)方法出來
@GetMapping(value = '')public String center() { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse(); //...}
通過參數(shù)直接獲取,只要在你的方法上加上參數(shù),Springboot就會幫你綁定,你可以直接使用。如果你的方法有其他參數(shù),把這兩個加到后面即可。
@GetMapping(value = '')public String center(HttpServletRequest request,HttpServletResponse response) { //...}
注入到類,這樣就不用每個方法都寫了
@Autowiredprivate HttpServletRequest request;@Autowiredprivate HttpServletResponse response;@GetMapping(value = '')public String center() { //...}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 一個 2 年 Android 開發(fā)者的 18 條忠告2. Vue實現(xiàn)仿iPhone懸浮球的示例代碼3. js select支持手動輸入功能實現(xiàn)代碼4. vue-drag-chart 拖動/縮放圖表組件的實例代碼5. 什么是Python變量作用域6. Spring的異常重試框架Spring Retry簡單配置操作7. Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進程8. PHP正則表達式函數(shù)preg_replace用法實例分析9. Android studio 解決logcat無過濾工具欄的操作10. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式
