Java Spring Boot: How to map my app root (“/”) to index.html?
It would have worked out of the box if you hadn't used @EnableWebMvc
annotation. When you do that you switch off all the things that Spring Boot does for you in WebMvcAutoConfiguration
. You could remove that annotation, or you could add back the view controller that you switched off:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
Why does root path in my controller maps to index.html in spring boot web application?
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
Add context path to Spring Boot application
Why are you trying to roll your own solution. Spring-boot already supports that.
If you don't already have one, add an application.properties
file to src\main\resources
. In that properties file, add 2 properties:
server.contextPath=/mainstay
server.port=12378
UPDATE (Spring Boot 2.0)
As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the contextPath
has been changed to the following:
server.servlet.context-path=/mainstay
You can then remove your configuration for the custom servlet container. If you need to do some post processing on the container you can add a EmbeddedServletContainerCustomizer
implementation to your configuration (for instance to add the error pages).
Basically the properties inside the application.properties
serve as a default you can always override them by using another application.properties
next to the artifact you deliver or by adding JVM parameters (-Dserver.port=6666
).
See also The Reference Guide especially the properties section.
The class ServerProperties
implements the EmbeddedServletContainerCustomizer
. The default for contextPath
is ""
. In your code sample you are setting the contextPath
directly on the TomcatEmbeddedServletContainerFactory
. Next the ServerProperties
instance will process this instance and reset it from your path to ""
. (This line does a null
check but as the default is ""
it always fail and set the context to ""
and thus overriding yours).
Related Topics
Java 8: Difference Between Method Reference Bound Receiver and Unbound Receiver
Changing the Shapes of Points in Scatter Plot
How to Speed Up the Gwt Compiler
Is the Order Guaranteed for the Return of Keys and Values from a Linkedhashmap Object
Converting Integer to String with Comma for Thousands
How to Compile a Java File with a Different Name Than the Class
Eclipse: Attach Source/Javadoc to a Library via a Local Property
How to Find Where Jdk Is Installed on My Windows MAChine
What Is the Default Access Modifier in Java
Mock a Constructor with Parameter
Best Way to Encode Text Data for Xml in Java
Sorting a List of Points with Java
How to Fetch Fetchtype.Lazy Associations with JPA and Hibernate in a Spring Controller
How to Add a Maven Dependency in Eclipse
List.Clear() VS List = New Arraylist<Integer>();
Java.Util.Zip.Zipexception: Error in Opening Zip File