Java

outside of an actual web request, or processing a request outside of the originally receiving thread? 오류에 대한 해결책.

JooKit 주킷 2022. 7. 6. 22:38
목차 접기
728x90
반응형
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.
	at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes

검색해보니 쓰레드세이프하지 않은 메서드에서 사용한다거나, 스케줄러 등 실질적인 request가 없는 곳에서 사용했을 때, static 메서드에서 사용했을 경우 등에서 발생하는 것 같다.

 

 

 

 

구글링 결과,,,,, stack overflow에서 해결책을 찾아버림,,.

 

구글링해서 얻은 결과는 바로 아래 코드.

private String getRemoteAddress() {
    RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
    if (attribs instanceof NativeWebRequest) {
        HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest();
        return request.getRemoteAddr();
    }
    return null;
}

 

 

내가 사용한 코드는 아래 코드 ..  현재는 문제가 없으나 앞으로도 문제가 없기를 바라본다,,.

RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = null;
if (attribs instanceof NativeWebRequest) {
    request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest();
}

 

 

728x90
반응형
LIST