Spring Boot Example: Simple REST API with GetMapping
Here's a basic Spring Boot application demonstrating a simple REST endpoint:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@GetMapping("/")
public String hello() {
return 'Hello, Spring Boot!';
}
}
This example features a straightforward Spring Boot application with a single GetMapping endpoint that returns 'Hello, Spring Boot!'.
@SpringBootApplication: Enables auto-configuration and component scanning.@RestController: Designates the class as a RESTful controller.@GetMapping("/"): Maps thehello()method to handle GET requests to the root URL ('/').
To run the application, use SpringApplication.run() in the main() method. This will start the Spring Boot application and listen for incoming requests.
Don't forget to include the required Spring Boot dependencies in your pom.xml or build.gradle file, based on your build tool.
This example provides a solid starting point for your Spring Boot journey. Feel free to ask if you have any more questions.
原文地址: https://www.cveoy.top/t/topic/qwyn 著作权归作者所有。请勿转载和采集!