1

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.