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

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

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

Also i added resource handler:

public void addResourceHandlers(ResourceHandlerRegistry registry) {

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.
, that request will be processed with my controller, then my controller sends redirect to
, this request goes to Spring's
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
not via spring servlet, but via default one (in my case
of undertow), but it's not possible, cause my app is not packaged as war and my
file located at

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

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
"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
's order to make resource mapping have higher priority that endpoint mapper?
Or is it possible to exclude request to
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