Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any possible way to use Controller and RestController annotations in one Spring Boot application?

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";
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add the spring-boot-starter-thymeleaf dependency. This automatically registers a view resolver for HTML files in src/main/resources/templates.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading