The Circular view path error in spring boot occurs when the request mapping url and the model view url are the same in a rest controller method and no ViewResolver is configured. The check your viewresolver setup! error occurs because the view path url points to the request mapping annotation url of the method in the rest controller class when the spring boot beans are loaded.
The controller receives a request and executes a method that is configured with the request mapping url. After the method execution is completed, the control is either forwarded to a UI page (jsp or html) or to another controller. If the control is redirected to the same method execution, then the request will run the method in an infinite loop.
The error javax.servlet.ServletException: Circular view path []: would dispatch back to the current handler URL [] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) is displayed in the control window when the request url is called in the spring boot program.
Exception
The full error stack trace of the error “Circular view path” is shown as shown below.
2020-07-23 22:09:10.021 ERROR 6829 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [welcome]: would dispatch back to the current handler URL [/welcome] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [welcome]: would dispatch back to the current handler URL [/welcome] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:317) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
Root Cause
In the spring boot program, the request url and the model view url are configured as the same url in the rest controller and no ViewResolver is configured, this error occurs. When the controller receives a request, it executes a method that is configured with the request mapping url.
Once the method execution is completed, the control is either forwarded to a UI page (jsp or html) or to another controller. If the control is redirected to the same method execution, then the request will run the method in an infinite loop.This infinite loop is causing this error “Circular view path”.
How to reproduce this issue
If a controller method is configured with a url in both as request mapping url and view path url, this error “Circular view path” can be reproduced. The example below is configured with the url “/welcome” in request mapping annotation and the view path url.
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
TestController.java
package com.yawintutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@RequestMapping(value="/welcome")
public ModelAndView welcomepage() {
return new ModelAndView("welcome");
}
}
Solution 1
If the rest controller method is expected to return a json response in the method, then add @ResponseBody annotation and return return the json string. The request is processed and returned as a reply. In this case, no redirect to any of the UI elements, such as jsp file, html file, etc. As the method executes once and return back, the error “”Circular view path” will be fixed
package com.yawintutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value="/welcome")
public @ResponseBody String welcomepage() {
return "{key:value}";
}
}
Solution 2
If the request mapping url and the view path url is same, this request creates an infinite loop. if the url in the RequestMapping annotation or the view path url is changed to an another name, this error “Circular view path” will be fixed.
package com.yawintutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@RequestMapping(value="/welcome")
public ModelAndView welcomepage() {
return new ModelAndView("hello world");
}
}
Solution 3
If you want to use thymeleaf as your ViewResolver, add the thymeleaf dependency to the pom.xml. Create an HTML file in the resource template folder. The thymeleaf framework redirects the control to the respected html file. The controller method will return the output of the html file.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
resources/templates/welcome.html
<html>
<body>
<center>
<h1>Hi Hello World</h1>
<h2>Welcome to spring boot</h2>
<a href="/">Home</a>
</center>
</body>
</html>
Solution 4
If you want to use jsp-jasper as your ViewResolver, add to pom.xml the tomcat-jasper dependency. In the application.properties files, add the configuration of the jsp folder. The controller class will execute the method, redirects to the jsp page. The output of the jsp file will be sent back as a response to the request.
pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
src/webapps/WEB-INF/jsp/welcome.jsp
<html>
<body>
<center>
<h1>Hi Hello World</h1>
<h2>Welcome to spring boot</h2>
<a href="/">Home</a>
</center>
</body>
</html>
Solution 5
Check the annotation of the @Controller in the java class file. If the @Controller annotation is used in your code, change the @RestController annotation. The @Controller class will create problems by redirecting the control to the respective files.