'Spring Boot: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'
I am trying to set home page of my application by using spring boot.. but I am getting the error as Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'
My code is as follow
RunApplication.java
@SpringBootApplication
public class RunApplication {
static Logger logger = Logger.getLogger(RunNavneetApplication.class);
public static void main(String[] args) {
SpringApplication.run(RunNavneetApplication.class, args);
}
}
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html
HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
WebConfig.java
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/productImages/**")
.addResourceLocations("file:/home/rahul/Desktop/product_images/")
.setCachePeriod(0);
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wocs</groupId>
<artifactId>REST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<service-version>0.1.36-SNAPSHOT</service-version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.wocs</groupId>
<artifactId>services</artifactId>
<version>${service-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
Please help me to fix this issue.. Thanks a lot in advance...
Solution 1:[1]
For me (spring-boot-starter-parent version 2.0.5.RELEASE) it worked changing the path in this way:
src/main/resources/WEB-INF/view/index.html ?
src/main/resources/META-INF/resources/view/index.html (for some reason it seemed not liking the WEB-INF name and it needed the nested resources directory).
It seems to work also with src/main/resources/resources/view/index.html
In these cases you don't even need to add the @EnableWebMvc annotation and implement the WebMvcConfigurer (prefix and suffix configured in application.properties are enough).
I add to my answer also a clarification about the MVC configuration taken from the Spring Boot Reference Guide:
27.1.1 Spring MVC Auto-configuration
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration [...] you can add your own
@Configurationclass of typeWebMvcConfigurerbut without@EnableWebMvc.If you want to take complete control of Spring MVC, you can add your own
@Configurationannotated with@EnableWebMvc.76.7 Switch off the Default MVC Configuration
The easiest way to take complete control over MVC configuration is to provide your own
@Configurationwith the@EnableWebMvcannotation. Doing so leaves all MVC configuration in your hands.
Solution 2:[2]
Removing of @EnableWebMvc annotation works for me in Spring Boot Project.
Solution 3:[3]
In your code you have defined suffix and prefix
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html
So all page in your request will convert to html file automatically,so you need to change return "index.html"; to return "index";
Change to
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
Solution 4:[4]
I was getting this error when doing a CURL request in my spring boot application:
{"timestamp":1566549840380,"status":500,"error":"Internal Server Error","message":"Could not resolve view with name 'createApiKey' in servlet with name 'dispatcherServlet'","path":"/createApiKey"}
I fixed it by changing from @Controller to @RestController (which is a combination of @Controller and @ResponseBody) like this:
@RestController
public class ApiKeyController {
...
@GetMapping("/createApiKey")
public DeferredResult<CreateApiKeyResp> createApiKey(@RequestParam long userId) {
...
Solution 5:[5]
In my case I was missing the thymeleaf dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Solution 6:[6]
Circular view path changes for spring-boot application getting jsp pages
Changes required in application.properties
spring.thymeleaf.mode=HTML5
spring.thymeleaf.enabled=false
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Changes in pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Ethan Burnside |
| Solution 5 | swissbuechi |
| Solution 6 | Sandeep Jain |
