When the jar present in class path, and when the application it automatically configures and starts a Tomcat server that is embedded inside this jar.
Contents
Bootstrapping a web server How to change default port number for web server
-
Add
spring-boot-starter-web maven dependency to your project, for the below example we are using version 2.7.6. For the latest version of the jar available, check maven repository herespring-boot-starter-web -
Next create a Java class with
main() method and annotate the class with@SpringBootApplication .
By declaring the class with@SpringBootApplication annotation, it automatically configures the app, and applies component scan on the current package and sub package classes. This is equivalent to annotating the class with@Configuration ,@EnableAutoConfiguration , and@ComponentScan . - When you launch the class, this automatically starts the embedded tomcat and binds to port 8080, which is the default port.
As you see in above example, Tomcat server started with default port number 8080,
if you want to change the port number, you can suply
-
You can supply
server.port as JVM argument,-Dserver.port=9090 -
You can supply
server.port as application argument,--server.port=9090 -
You can supply it through Spring's
application.properties file orapplication.yml file.
Below is an example specifying port number as 9090 in application.properties.
server.port=9090
Below is an example specifying port number as 9090 in application.yaml.