Replacing Tomcat: Configure Jetty with Spring Boot

 I this article we will learn how to configure Jetty with SpringBoot. I will not use Spring Initializr.

Jetty is fast and lightweight, making it ideal for high-performance applications.
Being modular, it allows enabling only the necessary components, optimizing resource usage.
Jetty efficiently handles high traffic volumes, enabling applications to scale seamlessly.

Add Dependencies to pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.egga</groupId>
<artifactId>examplejetty</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<dependencies>    
<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> 
<version>3.4.2</version>
</dependency>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- Jetty as embeded server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>3.4.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.4.1</version>   
</plugin>
</plugins>
</build></project>

Create application.properties file:

the file should inside  src/main/resources folder. The file contains its:

spring.application.name=demo

server.port=9090


Edit the Main Class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

Creating a Rest Controller:

package com.example.demo;

/**
*
* @author esteban
*/

import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
@RestController  

public class ExampleController {


@RequestMapping("/")  
public String hello()   
{  
return "Hello World form SpringBoot";  
}  
}


Finally, compile, run, and type the following address in your browser:

http://localhost:9090/

Finally We have a Jetty Web Application runnig with SpringBoot.


Comments

Popular posts from this blog

Building a Simple Web Form with Jetty and Servlets