Pregunta

Context:
In my Spring MVC application, I have an advisor that intercepts controller methods (My AOP works fine). In the advice class, I'm trying to extract information by calling HttpServletRequest.getRemoteUser(). Then I need to load user's authorities from DB.

My code
Interceptor class (implements MethodInterceptor):

private HttpServletRequest request = ((ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes()).getRequest();

In web.xml:

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Result/Problem:

I still get java.lang.IllegalStateException: No thread-bound request found, the exception still suggests using RequestContextListener or RequestContextFilter.

I have searched a lot on Stack Overflow and other places but no luck. Any help will be appreciated! (Please bear me for the old question:P)

Update:

I think I am referring to request attributes outside of an actual web request and that's why RequestContextListener doesn't help?

Is there any way I can still get the current request?

¿Fue útil?

Solución

For future developer's reference:
When you get a java.lang.IllegalStateException: No thread-bound request found exception which suggests using a RequestContextListener or a RequestContextFilter.

You have probably run into one of the two following situations.

Situation NO.1

You are not using org.springframework.web.servlet.DispatcherServlet (E.g. It's not in your web.xml or it is not a Spring MVC application).
Then you probably want to add RequestContextListener to your web.xml:

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

or RequestContextFilter (which does the same thing):

<filter>
  <filter-name>requestContextFilter</filter-name>
  <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>requestContextFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Then your code should just start working.

Situation NO.2

You want to get access to HttpServletRequest outside its original context, then following code should help:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

Make sure you put the code inside your method rather than inside the method's class!

The difference is as following:
Inside class: it is just an object needs to be created at initialization time, which will throw the exception because there is NO REQUEST AT ALL during this stage!
Inside method: it will be created at run time when there is an actual request coming in. This will work perfectly fine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top