Generiere ein simples Beispiel für die Verwendung von Spring Boot mit Java 21
- User
Generiere ein simples Beispiel für die Verwendung von Spring Boot mit Java 21
- Assistant
Hier ist ein einfaches Beispiel für eine Spring Boot-Anwendung mit Java 21. Wir erstellen eine REST-API mit einem einfachen Endpunkt.
- Zuerst die
pom.xmlfür Maven:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-demo</name> <description>Demo project for Spring Boot with Java 21</description> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>- Die Haupt-Anwendungsklasse (
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); } }- Ein einfacher 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; @RestController public class HelloController { // Verwendung eines Java 21 Record als DTO public record Greeting(String message) {} @GetMapping("/hello") public Greeting hello(@RequestParam(value = "name", defaultValue = "World") String name) { // Verwendung von Java 21 String Template (Preview-Feature) String message = STR."Hello, \{name}! Welcome to Spring Boot with Java 21!"; return new Greeting(message); } }- Eine einfache Testklasse (
HelloControllerTest.java):
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test void shouldReturnDefaultMessage() throws Exception { this.mockMvc.perform(get("/hello")) .andExpect(status().isOk()) .andExpect(jsonPath("$.message").value("Hello, World! Welcome to Spring Boot with Java 21!")); } @Test void shouldReturnCustomMessage() throws Exception { this.mockMvc.perform(get("/hello").param("name", "Alice")) .andExpect(status().isOk()) .andExpect(jsonPath("$.message").value("Hello, Alice! Welcome to Spring Boot with Java 21!")); } }Wichtige Hinweise zur Ausführung:
-
Stellen Sie sicher, dass Sie Java 21 installiert haben:
java --version -
Um die Anwendung zu starten, führen Sie aus:
mvn spring-boot:run -
Testen Sie die API mit curl oder einem Browser:
curl http://localhost:8080/hello # oder curl http://localhost:8080/hello?name=Alice -
Um die Tests auszuführen:
mvn test
Besondere Merkmale dieses Beispiels:
- Verwendet Java 21
- Nutzt Spring Boot 3.2.x (kompatibel mit Java 21)
- Demonstriert Java 21 Features wie Records
- Zeigt die Verwendung von String Templates (Preview-Feature in Java 21)
- Enthält grundlegende Integrationstests
- Verwendet moderne Spring Boot Annotationen
Hinweis zu Java 21 Preview-Features: Um Preview-Features wie String Templates zu nutzen, müssen Sie möglicherweise zusätzliche Compiler-Argumente hinzufügen. Fügen Sie dazu folgendes in die
pom.xmlein:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>--enable-preview</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>Und starten Sie die Anwendung mit:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="--enable-preview"Dies ist ein grundlegendes Beispiel, das Sie als Ausgangspunkt für komplexere Anwendungen verwenden können. Sie können es erweitern, indem Sie Datenbanken, Security, weitere Endpunkte und andere Spring-Features hinzufügen.
- Zuerst die
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.