Mike Flynn
Mike Flynn

Reputation: 24325

Get ServletContext from MessageContext in interceptor

How can I get an object of ServletContext from the MessageContext supplied in an interceptor? The TODO below is suppose to be a ServletContext.

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
        throws Exception {

    WebApplicationContext applicationContext =
            WebApplicationContextUtils.getWebApplicationContext(TODO);
            TestIdentitiesService service = applicationContext.getBean(TestIdentitiesService.class);

Upvotes: 0

Views: 1380

Answers (2)

VirtualTroll
VirtualTroll

Reputation: 3091

You can try the following:

@Autowired
private ServletContext context;

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
        throws Exception {

    WebApplicationContext applicationContext =
            WebApplicationContextUtils.getWebApplicationContext(context);
    TestIdentitiesService service =
    applicationContext.getBean(TestIdentitiesService.class);

Upvotes: 2

ikumen
ikumen

Reputation: 11643

You should be able to get a ServletContext with the following:

messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);

Upvotes: 1

Related Questions