Language: Java
Web
Spring Boot was created by Pivotal (now VMware) to simplify Spring application setup and development. Its goal was to reduce boilerplate code, provide sensible defaults, and enable rapid development of standalone, production-grade applications with minimal configuration.
Spring Boot is a framework for building production-ready Java applications quickly. It simplifies configuration, provides embedded servers, and integrates easily with Spring ecosystem projects like Spring Data, Spring Security, and Spring MVC.
Add Spring Boot starter dependencies in your pom.xmlAdd Spring Boot starter dependencies in your build.gradleSpring Boot allows developers to quickly create REST APIs, web applications, and microservices. It provides auto-configuration, embedded servers, and ready-to-use starters for common functionalities like data access, security, and messaging.
// MainApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}This is the entry point of a Spring Boot application. The `@SpringBootApplication` annotation enables auto-configuration and component scanning.
// HelloController.java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}Defines a REST endpoint `/hello` that returns a simple greeting string.
// User.java
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
// getters and setters
}
// UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {}Demonstrates creating an entity and repository using Spring Data JPA for database operations.
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.ResponseEntity;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception e) {
return ResponseEntity.status(500).body("Error: " + e.getMessage());
}
}Provides centralized exception handling for all controllers using `@ControllerAdvice`.
# application.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=passConfiguration properties for server port and database connection.
Use Spring Boot starters to avoid boilerplate dependency configurations.
Leverage `@Configuration` and `@Bean` for custom configurations.
Externalize configurations using `application.properties` or `application.yml`.
Write unit and integration tests using Spring Boot Test utilities.
Use profiles (`@Profile`) for environment-specific configurations.