Skip to content

Navigation Menu

Sign in
Sign up

Getting Started with Spring Boot

jfarcand edited this page Feb 18, 2026 · 7 revisions

Getting Started with Spring Boot 4.0

Build a real-time chat application with Atmosphere and Spring Boot 4.0 in minutes.

Prerequisites

  • JDK 21+
  • Maven 3.9+

1. Create the Project

Start from spring-boot-starter-parent 4.0.2 and add the Atmosphere starter:

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>4.0.2</version>
</parent>
<dependencies>
 <dependency>
 <groupId>org.atmosphere</groupId>
 <artifactId>atmosphere-spring-boot-starter</artifactId>
 <version>4.0.0</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>jakarta.inject</groupId>
 <artifactId>jakarta.inject-api</artifactId>
 <version>2.0.1</version>
 </dependency>
 <!-- For JSON encoding/decoding -->
 <dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 </dependency>
</dependencies>

2. Configure

In application.yml:

atmosphere:
 packages: com.example.chat

That's it — the starter auto-configures the AtmosphereServlet mapped to /atmosphere/*.

Available Properties

Property Default Description
atmosphere.servlet-path /atmosphere/* Servlet URL mapping
atmosphere.packages (none) Packages to scan for annotations
atmosphere.session-support false Enable HttpSession support
atmosphere.websocket-support (auto) Force WebSocket on/off
atmosphere.heartbeat-interval-in-seconds (default) Heartbeat frequency
atmosphere.broadcaster-class (default) Custom Broadcaster implementation
atmosphere.broadcaster-cache-class (default) Custom BroadcasterCache
atmosphere.init-params {} Additional servlet init parameters

3. Create the Chat Service

@ManagedService(path = "/atmosphere/chat",
 atmosphereConfig = "org.atmosphere.cpr.maxInactiveActivity=120000")
public class Chat {
 @Inject
 private AtmosphereResource r;
 @Inject
 private AtmosphereResourceEvent event;
 @Ready
 public void onReady() {
 // Connection is ready to receive messages
 }
 @Disconnect
 public void onDisconnect() {
 if (event.isClosedByClient()) {
 // Client closed the connection
 }
 }
 @Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class})
 public Message onMessage(Message message) {
 return message; // Broadcast to all connected clients
 }
}

See Understanding @ManagedService for the full annotation reference.

4. Create the Message Class

public class Message {
 private String message;
 private String author;
 private long time;
 public Message() { this("", ""); }
 public Message(String author, String message) {
 this.author = author;
 this.message = message;
 this.time = System.currentTimeMillis();
 }
 // Getters and setters
}

5. Create Encoder and Decoder

public class JacksonEncoder implements Encoder<Message, String> {
 @Inject private ObjectMapper mapper;
 @Override
 public String encode(Message m) {
 try { return mapper.writeValueAsString(m); }
 catch (IOException e) { throw new RuntimeException(e); }
 }
}
public class JacksonDecoder implements Decoder<String, Message> {
 @Inject private ObjectMapper mapper;
 @Override
 public Message decode(String s) {
 try { return mapper.readValue(s, Message.class); }
 catch (IOException e) { throw new RuntimeException(e); }
 }
}

6. Create the Application

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

7. Run

mvn spring-boot:run

Open http://localhost:8080 and start chatting!

Adding Rooms

See the Spring Boot Chat + Rooms sample for a complete example using RoomManager, presence events, and a REST API for room listing. Also see Understanding Rooms.

Actuator Integration

The starter includes optional health and metrics support:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
 endpoints:
 web:
 exposure:
 include: health,metrics
 endpoint:
 health:
 show-details: always

This exposes an Atmosphere health indicator at /actuator/health.

Spring Beans Integration

The AtmosphereFramework is available as a Spring bean. You can inject it to configure rooms, interceptors, or access the BroadcasterFactory programmatically:

@Configuration
public class RoomsConfig {
 private final AtmosphereFramework framework;
 public RoomsConfig(AtmosphereFramework framework) {
 this.framework = framework;
 }
 @Bean
 public RoomManager roomManager() {
 return RoomManager.getOrCreate(framework);
 }
}

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /