Skip to content
Advertisement

Why does root path in my controller maps to index.html in spring boot web application?

I’m testing a controller for a Spring Boot application. I want to map a resource to a path, which should be a part of my API. My controller is pretty specific about path:

@Controller
public class DefaultController
{
  @RequestMapping("${web-interface}")
  public String main()
  {
    return "index.html";
  }
}

Here ‘web-interface’ is a property, as specified in application.yml file

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/search-engine
    username: landsreyk
    password: 12345678
  jpa:
    database-platform: org.hibernate.dialect.MySQLDialect
    show-sql: false
    hibernate:
      ddl-auto: none
web-interface: /admin

Expected behavior:

path: localhost:8080/admin maps to index.html resource

root path: localhost:8080/ maps to nothing, i.e. 404 error.

Actual behavior:

path: ‘/admin’ maps to index.html

path: ‘/’ also maps to index.html

But why? Shouldn’t I just see “Whitelabel Error Page”. There is no controller, which maps root path to index.html file. It doesn’t make any sense.

By the way, here is my project structure.

enter image description here

Solution:

Rename index.html to any other name, like main.html and root path ‘/’ will no longer map to that resource.

Advertisement

Answer

Root path “/” by defaults maps to index.html. It is standard for all languages and frameworks. index.html is meant to be entry point for your application

Advertisement