Hier ist ein einfaches Beispiel für eine Spring Boot-Anwendung mit Java 21, die einen REST-Endpunkt bereitstellt:
1. Projektstruktur
src
└── main
└── java
└── com
└── example
└── demo
├── DemoApplication.java
└── HelloController.java
2. Maven Konfiguration (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.example</groupId>
<artifactId>java21-spring-demo</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version> <!-- Unterstützt Java 21 -->
</parent>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. Hauptanwendung (DemoApplication.java)
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);
}
}
4. REST-Controller (HelloController.java)
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
// Einfacher Endpunkt mit Java 21 String Templates (Preview)
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return STR."Hello \{name}!";
}
// Beispiel mit Java 21 Sequenced Collections
@GetMapping("/recent")
public String getRecentItem() {
List<String> items = List.of("Apple", "Banana", "Cherry");
return items.getLast(); // Neue Methode in Java 21
}
}
5. Anwendung ausführen
- Mit Maven:
mvn spring-boot:run
- Oder direkt in der IDE:
DemoApplication als Java-Klasse ausführen
6. Endpunkte testen
Wichtige Java 21 Features im Beispiel:
-
String Templates (Preview):
STR."Hello \{name}!"
- Verbesserte String-Interpolation (Preview-Feature)
-
Sequenced Collections:
items.getLast()
- Neue Standardmethoden für Collections
Voraussetzungen:
- Java 21 JDK (Download)
- Maven 3.6+ oder IDE (IntelliJ IDEA, Eclipse, etc.)
- Spring Boot 3.2+ (unterstützt Java 21)
Besonderheiten:
- Die Preview-Features müssen in der IDE aktiviert werden (z.B. in IntelliJ: Build Tools → Maven → Runner:
-Dspring-boot.run.jvmArguments="--enable-preview")
- Für die Verwendung von Preview-Features in Produktion ist besondere Vorsicht geboten
Dieses Beispiel zeigt:
- Einrichtung eines minimalen Spring Boot 3.2 Projekts
- Integration von Java 21 Features
- Erstellung eines REST-Controllers
- Verwendung moderner Java 21 APIs