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

Time:12-05

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.

CodePudding user response:

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

CodePudding user response:

It is the default behavior for every entry point. The DefaultController implements the default behavior, that's why it doesn't matter if you call "/" or "/root". For further info: https://docs.spring.io/spring-boot/docs/2.6.1/reference/htmlsingle/#web.servlet.spring-mvc.welcome-page

  • Related