-
-
Notifications
You must be signed in to change notification settings - Fork 761
Getting Started with Spring Boot
Build a real-time chat application with Atmosphere and Spring Boot 4.0 in minutes.
- JDK 21+
- Maven 3.9+
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>
In application.yml:
atmosphere: packages: com.example.chat
That's it — the starter auto-configures the AtmosphereServlet mapped to /atmosphere/*.
| 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 |
@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.
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 }
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); } } }
@SpringBootApplication public class ChatApplication { public static void main(String[] args) { SpringApplication.run(ChatApplication.class, args); } }
mvn spring-boot:run
Open http://localhost:8080 and start chatting!
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.
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.
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); } }