Serhii Hromovyi Serhii Hromovyi - 1 year ago 152
Java Question

Circular view in spring controller

I'm trying to serve static resources in my Spring controller.
I have a

index.html
file, that should be returned to each request.
My controller is:

@Controller
public class IndexController {
@RequestMapping(value = "/**", method = RequestMethod.GET)
public String index() {
return "index.html";
}
}


Also i added resource handler:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/");
}


But at the end i have following exception:


Circular view path [/index.html]: would dispatch back to the current handler URL [/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)


I understand, that in my configuration all requests will be handled with my Spring controller.

So when i request for, e.g.
http://localhost/someText
, that request will be processed with my controller, then my controller sends redirect to
//localhost/index.html
, this request goes to Spring's
DispatcherServlet
and then it redirected to my controller again, that causes exception above.

Also I already tried some options: to make web filter, that will process my request to
/index.html
not via spring servlet, but via default one (in my case
DefaultServlet
of undertow), but it's not possible, cause my app is not packaged as war and my
index.html
file located at
/src/java/resources
.

Actually I found, that main problem for me is that in spring's
DispatcherServlet
handler for my request (
//localhost/index.html
) detected as my controller, not resource handler.

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}


So maybe there is a way to change
HandlerMapping
's order to make resource mapping have higher priority that endpoint mapper?
Or is it possible to exclude request to
/index.html
from my controller to be able to handle it with spring resource handlers?

Answer Source

I've figured out that in following way: I defined requests, that should be processed in some exact controllers. Then i created Filter, that adds to all other requests prefix "/root" in path. And i made a controller, that processes all "/root/**" requests serving my index.html.

So, basically, when i request "/api/someApi" - so it goes to my controller with API stuff. But when i request "/someStuff/" - in filter it becomes "/root/someStuff/" - so it goes to my controller that returns "/index.html".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download