Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular View path error Spring boot

I am very new to Spring. I am trying to build a MVC application using Spring Boot which shows a list of products. But i am getting the below error:

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

Here is controller:

package com.springframeworkguru.controllers;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;  import com.springframeworkguru.services.ProductService;       @Controller     public class ProductController {          private ProductService productService;          @Autowired         public void setProductService(ProductService productService) {             this.productService = productService;         }          @RequestMapping("/products")         public String listProducts(Model model){              model.addAttribute("products", productService.listAllProducts());              return "products";         }      } 

This is the main class:

package com.springframeworkguru;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer;  import com.springframeworkguru.controllers.ProductController;  @SpringBootApplication public class SpringmvcApplication extends SpringBootServletInitializer{       public static void main(String[] args) {         SpringApplication.run(SpringmvcApplication.class, args);     } } 

and products.html:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head>     <title>Spring Core Online Tutorial - List Products</title>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>      <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"           th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"           rel="stylesheet" media="screen"/>      <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"             th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>      <link href="../css/spring-core.css"           th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/> </head> <body> <div class="container">     <div th:if="${not #lists.isEmpty(products)}">         <h2>Product List</h2>         <table class="table table-striped">             <tr>                 <th>Id</th>                 <th>Description</th>                 <th>Price</th>                 <th>Image URL</th>                 <th>List</th>             </tr>             <tr th:each="product : ${products}">                 <td th:text="${product.id}"></td>                 <td th:text="${product.description}"></td>                 <td th:text="${product.price}"></td>                 <td th:text="${product.imageUrl}"></td>                 <td><a th:href="${'/product/' + product.id}">View</a> </td>             </tr>         </table>     </div> </div>  </body> </html> 

The products.html is in /static folder. Also, I am using Eclipse Kepler.

like image 890
sohan Avatar asked Apr 18 '16 15:04

sohan


People also ask

What is circular view path error?

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!

What is difference between @controller and @RestController?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.

What is spring ViewResolver?

The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.


2 Answers

Adding spring-boot-starter-thymeleaf dependency solved the problem.

So add this to your pom.xml file:

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 

Update: If you are working with Eclipse and you are using Gradle, this might not work. The reason is if you haven't imported the project as 'gradle project' Eclipse wont detect thymeleaf. So here's the solution:

Step1 : Run "gradle eclipse" on command line.

Step2 : Run "gradle wrapper"

Step3 : In eclipse import as gradle project (before this remove the already imported project)

Step4 : Now run using eclipse

Step5 : Enjoy!

like image 107
user18853 Avatar answered Oct 02 '22 15:10

user18853


You can also be here because:

  1. You forgot to put the @RestController of your rest controller over the class

  2. You set @Controller instead of @RestController

like image 34
S.Dayneko Avatar answered Oct 02 '22 16:10

S.Dayneko