One of the reason this error coccured is that you forgot the @RestController annotation on top of your controller class import, import org.springframework.web.bind.annotation.RestController;
Refer the simple example below:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
The other cause of this issue is that you might be missing the @Configuration annotation in the MvcConfig class which basically does the mapping for ViewControllers and setViewNames.
Content of the file :
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
**@Configuration**
public class MvcConfig implements WebMvcConfigurer{
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/").setViewName("login");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/dashboard").setViewName("dashboard");
}
}
I hope this helps.