No thread-bound request found

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

分析

該錯(cuò)誤信息在RequestContextHolder這個(gè)類中,詳細(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)用該方法要慎重,為了避免出錯(cuò)鞠眉,可以再封裝一下

package com.yzy.common.core.interceptors;

import cn.hutool.core.util.StrUtil;
import com.yzy.common.constant.AuthConstant;
import com.yzy.common.core.local.LocalUser;
import com.yzy.common.core.local.UserIdentityBO;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 當(dāng)拿不到request時(shí)薯鼠,創(chuàng)建NonWebRequestAttributes來實(shí)現(xiàn)RequestAttributes,然后加一個(gè)非空判斷繞過強(qiáng)轉(zhuǎn)的地方械蹋,這樣就能正常的調(diào)用其他服務(wù)了
 */
public class FeignRequestHeaderInterceptor implements RequestInterceptor {
    public FeignRequestHeaderInterceptor(){

    }

    @Override
    public void apply(RequestTemplate requestTemplate) {
    //修改前
//        RequestAttributes requestAttributes =  RequestContextHolder.currentRequestAttributes();
//修改后
        HttpServletRequest httpServletRequestSafely = getHttpServletRequestSafely();
        
        if (httpServletRequestSafely != null) {
            HttpServletRequest request = ((ServletRequestAttributes) httpServletRequestSafely).getRequest();
            requestTemplate.header(AuthConstant.USER_TOKEN_HEADER, request.getHeader(AuthConstant.USER_TOKEN_HEADER));
        }
        UserIdentityBO userIdentityBO = LocalUser.getLocalUser();
        if (userIdentityBO==null){
            return;
        }
        if(!StrUtil.isEmpty(userIdentityBO.getScope())){
            requestTemplate.header(AuthConstant.CURRENT_SCOPE, userIdentityBO.getScope());
        }
        if (null != userIdentityBO.getUid() && 0 != userIdentityBO.getUid()) {
            requestTemplate.header(AuthConstant.CURRENT_USER, String.valueOf(userIdentityBO.getUid()));
        }
        if(null != userIdentityBO.getIdentity() ){
            requestTemplate.header(AuthConstant.CURRENT_IDENTITY, String.valueOf(userIdentityBO.getIdentity()));
        }
    }

    public HttpServletRequest getHttpServletRequestSafely() {
        try {
            RequestAttributes requestAttributesSafely = this.getRequestAttributesSafely();
            return requestAttributesSafely instanceof NonWebRequestAttributes ? null : ((ServletRequestAttributes)requestAttributesSafely).getRequest();
        } catch (Exception var2) {
            return null;
        }
    }

    public RequestAttributes getRequestAttributesSafely() {
        Object requestAttributes = null;

        try {
            requestAttributes = RequestContextHolder.currentRequestAttributes();
        } catch (IllegalStateException var3) {
            requestAttributes = new NonWebRequestAttributes();
        }

        return (RequestAttributes)requestAttributes;
    }

}

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

NonWebRequestAttributes

package com.yzy.common.core.interceptors;

import org.springframework.web.context.request.RequestAttributes;

public class NonWebRequestAttributes implements RequestAttributes {
    @Override
    public Object getAttribute(String s, int i) {
        return null;
    }

    @Override
    public void setAttribute(String s, Object o, int i) {

    }

    @Override
    public void removeAttribute(String s, int i) {

    }

    @Override
    public String[] getAttributeNames(int i) {
        return new String[0];
    }

    @Override
    public void registerDestructionCallback(String s, Runnable runnable, int i) {

    }

    @Override
    public Object resolveReference(String s) {
        return null;
    }

    @Override
    public String getSessionId() {
        return null;
    }

    @Override
    public Object getSessionMutex() {
        return null;
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市哗戈,隨后出現(xiàn)的幾起案子郊艘,更是在濱河造成了極大的恐慌,老刑警劉巖唯咬,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纱注,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡胆胰,警方通過查閱死者的電腦和手機(jī)奈附,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來煮剧,“玉大人斥滤,你說我怎么就攤上這事∶阒眩” “怎么了佑颇?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長草娜。 經(jīng)常有香客問我挑胸,道長,這世上最難降的妖魔是什么宰闰? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任茬贵,我火速辦了婚禮簿透,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘解藻。我一直安慰自己老充,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布螟左。 她就那樣靜靜地躺著啡浊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪胶背。 梳的紋絲不亂的頭發(fā)上巷嚣,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音钳吟,去河邊找鬼廷粒。 笑死,一個(gè)胖子當(dāng)著我的面吹牛红且,可吹牛的內(nèi)容都是我干的评雌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼直焙,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼景东!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起奔誓,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤斤吐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后厨喂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體和措,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年蜕煌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了派阱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡斜纪,死狀恐怖贫母,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情盒刚,我是刑警寧澤腺劣,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站因块,受9級(jí)特大地震影響橘原,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一趾断、第九天 我趴在偏房一處隱蔽的房頂上張望拒名。 院中可真熱鬧,春花似錦芋酌、人聲如沸增显。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甸怕。三九已至甘穿,卻和暖如春腮恩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背温兼。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工秸滴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人募判。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓荡含,卻偏偏與公主長得像,于是被迫代替她去往敵國和親届垫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子释液,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

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