Embedded Servers in Spring Boot

Embedded Servers in Spring Boot

Run the Spring Boot Application without a web server

ยท

3 min read

Traditionally, We are used to deploy our web applications into servlet containers also known as Web servers. There are a wide variety of web servers available in the market like Apache Tomcat, IBM Websphere, Oracle Weblogic etc.

What you will learn from this blog?

  1. What is an Embedded Server

  2. Embedded Servers in Spring Boot

  3. Configuration in Spring Boot

What is an Embedded Server

To deploy a web application, We generally require a Virtual machine and follow the below steps

  1. Install Java

  2. Install the web server

  3. Deploy the War(Web archive) file

We can simplify these steps by embedding the web server as part of the deployable application and we would only require a virtual machine where Java is installed to execute/run the application.

In Java Applications, A web server embedded with the application is termed as JAR (Java Archive).

Embedded Servers in Spring Boot

One of the major advantages of using Spring Boot is, it provides embedded servers.

Spring Boot allows developers to easily build applications using one of the below web servers

  1. Tomcat

  2. Undertow

  3. Jetty

By default, Spring Boot uses the Tomcat server as the embedded server.

We can change the embedded web server by excluding the tomcat dependency and by adding either Undertow or Jetty as a dependency.

Configuration in Spring Boot

Let us develop a simple Spring Boot Application using Spring Initializr.

Let us start the Spring Boot Application and check the server name in the console.

By default, Spring Boot uses Apache Tomcat as the embedded server.

How can I find the server name, If I don't have access to the console?

Let us develop a Simple Rest endpoint that gives us the embedded server information. We can achieve this by developing a Rest controller as shown below.

This endpoint is developed for demo purposes and may not be used in production-grade applications.

How to change the embedded web server

The Spring Web dependency in the pom.xml has the tomcat dependency embedded by default. This is why spring boot uses tomcat as the default web server.

Let us exclude the tomcat dependency and add the Undertow web server as a dependency in the pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

Start the Spring Boot Application and hit the Rest Endpoint. Verify the server in both the endpoint response and also the console.

We can also change the port number by using the property server.port in application.properties file

Now, Remove the Undertow dependency and add the Jetty as a web server in pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

We can verify the server name in both the console and Rest API response.

Until here, We have started the application using the IDE. We can also start the application by invoking the JAR file from the command line.

Since the project build tool is maven. Execute the command mvn clean install which will generate the JAR file in the target folder.

We can invoke the JAR by using the command java -jar embedded-server-demo.jar

Congratulations... Now we have learned about using the embedded web servers in Spring Boot and also changing them when required.

The source code is available on GitHub mentioned below

You can follow me on Twitter and Linkedin to stay updated with my blogs. Happy Learning .. ๐Ÿ˜Š๐Ÿ‘

ย