FMC
FMC

Reputation: 660

Override ResourceHttpRequestHandler Spring MVC

I am trying to override ResourceHttpRequestHandler in my Spring MVC application.

I have the following class so far:

@Controller
public class ResourceHttpRequestHandlerReplacer extends ResourceHttpRequestHandler implements BeanFactoryPostProcessor {

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    super.handleRequest(request, response);
}

public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
        throws BeansException {
    String[] names = factory.getBeanNamesForType(ResourceHttpRequestHandler.class);

    for (String name : names) {
        BeanDefinition bd = factory.getBeanDefinition(name);
        bd.setBeanClassName("com.project.controllers.ResourceHttpRequestHandlerReplacer");
    }
}

}

Right now its supposed to just call the super handleRequest so nothing should have changed but I am getting this message:

Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@16943e88]]] does not run within a ServletContext. Make sure the object is fully configured!

I am trying to override so that I can implement some custom logic around trying to find resources that aren't static but aren't mapped by a controller either as the user will be able to define URLs for the pages in their CMS.

Can anyone advise where ive gone wrong?

Thanks

Upvotes: 0

Views: 1877

Answers (1)

David Florez
David Florez

Reputation: 1436

looks like your ResourceHttpRequestHandlerReplacer is not aware of the servletContext, which is rare since you are extending it from ResourceHttpRequestHandler. Try adding this to your class

@Autowired
ServletContext servletContext;

Upvotes: 2

Related Questions