Spring Boot: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'
Asked Answered
I

5

14

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...

Internalcombustion answered 12/7, 2018 at 10:36 Comment(1)
Did you solve your problem ?Esoteric
T
8

Removing of @EnableWebMvc annotation works for me in Spring Boot Project.

Tolkan answered 18/6, 2021 at 18:42 Comment(0)
S
7

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.htmlsrc/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 @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated 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 @Configuration with the @EnableWebMvc annotation. Doing so leaves all MVC configuration in your hands.

Spokesman answered 16/9, 2018 at 1:0 Comment(0)
K
1

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) {
        ...
Kerf answered 23/8, 2019 at 23:21 Comment(0)
U
0

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";
    }
}
Unknow answered 12/7, 2018 at 10:58 Comment(3)
javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'Internalcombustion
I tried but error is same... I have edited the question... please checkInternalcombustion
Request mapping should be with @Controller anotation likeFifteen
M
0

In my case I was missing the thymeleaf dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Mycenae answered 17/3, 2022 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.