I have my application, which is running on Spring Boot. In my application, I have two controllers, @RestController and @Controller.
Rest is for returning JSON data and Controller is for HTML pages. When I try to use these two boys in my one project, it always gives me an error code:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
My HTML page is located in resources:/templates.
RequestMappingClass:
@RestController
@RequestMapping(value = "/api/")
public class PageRestController {
@GetMapping(value = "/getUserData")
public ResponseEntity<User> getUser() {
User user = new User();
user.setName("Nika");
user.setSurname("Beridze");
user.setAge(19);
return ResponseEntity.ok().body(user);
}
}
Controller:
@Controller
public class PageController {
@GetMapping("/index")
public String index() {
return "index";
}
}
>Solution :
Add the spring-boot-starter-thymeleaf dependency. This automatically registers a view resolver for HTML files in src/main/resources/templates.