Error : No thread-bound request found

 

Caused by: java.lang.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.Caused by: java.lang.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. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]

DTO나 Static 메서드에서 파라미터로 받아오지 않는

쓰레드세이프하지 않게 만들어놓으면 발생하는 문제

(리퀘스트 홀더나 세션에 리퀘스트 관련 정보가 저장되어 있다거나)

HttpRequest에서 들어오는 파라미터 등은 쓰레드세이프하게 사용.

 

이번 오류는 DTO에 RequestContextHolder.currentRequestAttributes() 가 들어있어서 발생.

public class SiteDTO implements Serializable {
    private final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    private final HttpServletRequest req = servletRequestAttributes.getRequest();

~~~~~
}

Mybatis로 DB의 값을 받아올 때 사용하는 DTO인데 RequestContextHolder를 사용한다.

일반적인 상황에서는문제가 없지만 Request가 생성되기 전에 Security 에서 DB를 호출하는 경우 문제가 생길 수 있다.

Filter의 앞단에서 아래같은걸 해주거나…

https://stackoverflow.com/questions/43404300/no-thread-bound-request-found

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;

@Provider
public class RequestFilter implements ContainerRequestFilter {
    private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =
            RequestFilter.class.getName() + ".REQUEST_ATTRIBUTES";

    @Context private HttpServletRequest httpRequest;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        ServletRequestAttributes attributes = new ServletRequestAttributes(httpRequest);
        httpRequest.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
        LocaleContextHolder.setLocale(httpRequest.getLocale());
        RequestContextHolder.setRequestAttributes(attributes);
    }

}

근데 뭐가 됐건 DTO에다가 Static 호출을 넣으면 안된다.