programing

자바 lang잘못된 상태 예외:스레드 바인딩 요청을 찾을 수 없습니다. 측면에서 예외가 있습니다.

bestprogram 2023. 9. 19. 21:19

자바 lang잘못된 상태 예외:스레드 바인딩 요청을 찾을 수 없습니다. 측면에서 예외가 있습니다.

다음은 제 측면입니다.

    @Configurable
    @Aspect
    public class TimingAspect {

        @Autowired
        private HttpServletRequest httpServletRequest;

        // Generic performance logger for any mothod
        private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint, String remoteAddress) {
            StringBuilder tag = new StringBuilder();
            if (joinPoint.getTarget() != null) {
                tag.append(joinPoint.getTarget().getClass().getName());
                tag.append(".");
            }
            tag.append(joinPoint.getSignature().getName());
            StopWatch stopWatch = new StopWatch(tag.toString());
            Object result = joinPoint.proceed(); // continue on the intercepted method
            stopWatch.stop();

            PerformanceUtils.logInPerf4jFormat(stopWatch.getStartTime(), stopWatch.getElapsedTime(), stopWatch.getTag(), stopWatch.getMessage(), remoteAddress);
            return result;
        }

        @Around("execution(* $$$.$$$.$$$.api.controller.*.*(..))")
        public Object logAroundApis(ProceedingJoinPoint joinPoint) throws Throwable {
            String remoteAddress = null;
            if (httpServletRequest != null) {
               remoteAddress = httpServletRequest.getRemoteAddr();
            }
            return logPerfomanceInfo(joinPoint, remoteAddress);
        }

        @Around("execution(* $$$.$$$.$$$.$$$.$$$.$$$.*(..))")
        public Object logAroundService(ProceedingJoinPoint joinPoint) throws Throwable {
            String remoteAddress = null;
            if (httpServletRequest != null) {
                remoteAddress = httpServletRequest.getRemoteAddr();
            }
            return logPerfomanceInfo(joinPoint, remoteAddress);
        }

컴파일 시간 오류는 발생하지 않지만 jetty 서버를 시작할 때 다음 예외가 발생합니다.

중첩 예외는 java. lang입니다.잘못된 상태 예외:스레드 바인딩된 요청을 찾을 수 없습니다.실제 웹 요청 외부의 요청 속성을 말하는 것입니까, 아니면 원래 수신 스레드 외부의 요청을 처리하는 것입니까?웹 요청 내에서 실제로 작동하고 있지만 여전히 이 메시지를 받는 경우 코드가 DispatcherServlet/DispatcherPortlet:이 경우 RequestContextListener 또는 RequestContextFilter를 사용하여 현재 요청을 노출합니다.

여기서 주의할 점은 "logAroundService" 메서드를 제거하면 예외가 발생하지 않습니다.

당신은 자동 와이어를 사용해서는 안 됩니다.HttpServletRequest이것이 실행 내에서 호출되는 클래스에 대해서만 실행 가능하도록 당신의 측면을 묶을 것이기 때문에.HttpServletRequest.

대신 사용합니다.RequestContextHolder당신이 필요할 때 요청을 받기 위해서 입니다.

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

@M. 데이넘의 대답은 저에게 통하지 않습니다.대신 이 코드를 사용합니다.

RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null) {
    HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
    return request.getRemoteAddr();
}

RequestContextListener에 대한 빈을 만듭니다.나는 HttpServletRequest 자동배선에 대해 같은 오류를 받았고 다음 두 줄의 코드가 나에게 적합합니다.

@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}

오류 메시지에 표시된 대로:이 경우 RequestContextListener 또는 RequestContextFilter를 사용하여 현재 요청을 노출합니다.

수정하려면 RequestContextListener 수신기를 web.xml 파일에 등록합니다.

<web-app ...>
   <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
   </listener>
</web-app>

당신의 포인트 컷 표현은 기본적으로 모든 콩을 대리하고 그 조언을 적용하는 것입니다.어떤 콩들은 존재하고 그것의 맥락 밖에서 작동합니다.HttpServletRequest. 이는 검색할 수 없음을 의미합니다.

주사만 할 수 있습니다.HttpServletRequest서블릿 컨테이너 요청이 처리 스레드를 통과하는 장소에 있습니다.

2021년 전후에 인터넷에서 오류 메시지를 검색해서 왔을 경우...저도 그랬었는데 결국 두 개가 있다는 걸 깨달았어요.@Configuration시행한 수업WebMvcConfigurer. 중복을 제거하면 문제가 해결됩니다.

다음을 추가하여 해결했습니다.

@Around(value = "@annotation(JwtSecure)")
@Pointcut("within(@org.springframework.stereotype.Repository *)"
        + " || within(@org.springframework.stereotype.Service *)"
        + "|| within(@org.springframework.stereotype.Component *)"
        + " || within(@org.springframework.web.bind.annotation.RestController *)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

우편배달부/웹에서 요청을 받고 새 스레드를 생성하려고 할 때 봄 부팅 시 이 문제가 발생하는 경우(컴플리트를 사용할 수 있음)Future) 응답을 받습니다.그러면 sessionId와 같이 원래 요청과 관련된 생성된 스레드 내에서 무언가를 가져오려고 할 수도 있습니다.대신 메인 스레드에서 이들(sessionId 및 기타 매개 변수)을 가져와야 하며 Completeable에서 호출할 원래 요청 메서드 인수와 관련된 sessionId 및 기타 매개 변수를 전달해야 합니다.미래.원래 요청에서 sessionId를 추출하여 someMethodCall()에 매개변수로 전달하는 것과 동일한 스크린샷을 공유했습니다.

enter image description here

언급URL : https://stackoverflow.com/questions/24025924/java-lang-illegalstateexception-no-thread-bound-request-found-exception-in-asp