No thread-bound request found異常

本文主要研究下spring mvc的No thread-bound request found異常

分析

該錯誤信息在RequestContextHolder這個類中,詳細(xì)如下:
spring-web-4.3.7.RELEASE-sources.jar!/org/springframework/web/context/request/RequestContextHolder.java

public abstract class RequestContextHolder  {

    private static final boolean jsfPresent =
            ClassUtils.isPresent("javax.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());

    private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
            new NamedThreadLocal<RequestAttributes>("Request attributes");

    private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
            new NamedInheritableThreadLocal<RequestAttributes>("Request context");

    /**
     * Return the RequestAttributes currently bound to the thread.
     * <p>Exposes the previously bound RequestAttributes instance, if any.
     * Falls back to the current JSF FacesContext, if any.
     * @return the RequestAttributes currently bound to the thread
     * @throws IllegalStateException if no RequestAttributes object
     * is bound to the current thread
     * @see #setRequestAttributes
     * @see ServletRequestAttributes
     * @see FacesRequestAttributes
     * @see javax.faces.context.FacesContext#getCurrentInstance()
     */
    public static RequestAttributes currentRequestAttributes() throws IllegalStateException {
        RequestAttributes attributes = getRequestAttributes();
        if (attributes == null) {
            if (jsfPresent) {
                attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();
            }
            if (attributes == null) {
                throw new IllegalStateException("No thread-bound request found: " +
                        "Are you referring to request attributes outside of an actual web request, " +
                        "or processing a request outside of the originally receiving thread? " +
                        "If you are actually operating within a web request and still receive this message, " +
                        "your code is probably running outside of DispatcherServlet/DispatcherPortlet: " +
                        "In this case, use RequestContextListener or RequestContextFilter to expose the current request.");
            }
        }
        return attributes;
    }

    /**
     * Return the RequestAttributes currently bound to the thread.
     * @return the RequestAttributes currently bound to the thread,
     * or {@code null} if none bound
     */
    public static RequestAttributes getRequestAttributes() {
        RequestAttributes attributes = requestAttributesHolder.get();
        if (attributes == null) {
            attributes = inheritableRequestAttributesHolder.get();
        }
        return attributes;
    }

    //...
}

解決

在非web下訪問了RequestContextHolder.currentRequestAttributes()導(dǎo)致蚯嫌,因此在service層方法里頭調(diào)用該方法要慎重卑吭,為了避免出錯芽淡,可以再封裝一下

    public RequestAttributes getRequestAttributesSafely(){
        RequestAttributes requestAttributes = null;
        try{
            requestAttributes = RequestContextHolder.currentRequestAttributes();
        }catch (IllegalStateException e){
            requestAttributes = new NonWebRequestAttributes();
        }
        return requestAttributes;
    }

NonWebRequestAttributes實現(xiàn)RequestAttributes接口,重寫的方法都為空即可

小結(jié)

對于在service層使用RequestContextHolder的方法要小心陨簇,除了controller層外,調(diào)度任務(wù)等也可能調(diào)用service層的方法迹淌。

doc

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末河绽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子唉窃,更是在濱河造成了極大的恐慌耙饰,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纹份,死亡現(xiàn)場離奇詭異苟跪,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蔓涧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門件已,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人元暴,你說我怎么就攤上這事篷扩。” “怎么了茉盏?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵鉴未,是天一觀的道長。 經(jīng)常有香客問我鸠姨,道長铜秆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任讶迁,我火速辦了婚禮连茧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己梅屉,他們只是感情好值纱,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著坯汤,像睡著了一般虐唠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惰聂,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天疆偿,我揣著相機(jī)與錄音,去河邊找鬼搓幌。 笑死杆故,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的溉愁。 我是一名探鬼主播处铛,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拐揭!你這毒婦竟也來了撤蟆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤堂污,失蹤者是張志新(化名)和其女友劉穎家肯,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盟猖,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡讨衣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了式镐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片反镇。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖娘汞,靈堂內(nèi)的尸體忽然破棺而出愿险,到底是詐尸還是另有隱情,我是刑警寧澤价说,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布辆亏,位于F島的核電站,受9級特大地震影響鳖目,放射性物質(zhì)發(fā)生泄漏扮叨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一领迈、第九天 我趴在偏房一處隱蔽的房頂上張望彻磁。 院中可真熱鬧碍沐,春花似錦、人聲如沸衷蜓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽磁浇。三九已至斋陪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間置吓,已是汗流浹背无虚。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留衍锚,地道東北人友题。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像戴质,于是被迫代替她去往敵國和親度宦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容