SpringBoot
4/16/20252 min read
πΉ 1. What is Spring Boot 3.x and how is it different from 2.x?
β Answer:
Spring Boot 3.x is built on Spring Framework 6 and requires Java 17+.
It supports Jakarta EE 10 (javax β jakarta).
Embraces native image support via GraalVM.
Full support for Observability via Micrometer and Actuator enhancements.
Removed deprecated classes/APIs from 2.x.
πΉ 2. How to enable auto-configuration in Spring Boot?
β Answer:
Spring Boot uses @SpringBootApplication which includes @EnableAutoConfiguration.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
You can exclude a configuration:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
πΉ 3. How do you handle exceptions globally in Spring Boot?
β Answer:
Use @ControllerAdvice and @ExceptionHandler.
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<String> handleNotFound(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
}
πΉ 4. How does Spring Boot support Observability (Micrometer + Actuator)?
β Answer:
Spring Boot Actuator 3.x provides:
Tracing with OpenTelemetry.
Micrometer metrics (CPU, memory, requests).
/actuator/metrics, /actuator/health, /actuator/prometheus.
Enable in application.yml:
management:
endpoints:
web:
exposure:
include: "*"
tracing:
enabled: true
metrics:
export:
prometheus:
enabled: true
πΉ 5. What is the difference between @Component, @Service, and @Repository?
β Answer:
All are stereotype annotations for Spring-managed beans.
Annotation
Purpose
@Component
Generic component
@Service
Business logic or service layer
@Repository
DAO layer, auto exception translation
πΉ 6. How do you write a REST Controller in Spring Boot?
β Example:
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return ResponseEntity.ok(new User(id, "Lokesh"));
}
}
πΉ 7. How to connect Spring Boot with a database (JPA + H2)?
β Example:
application.yml
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
enabled: true
jpa:
hibernate:
ddl-auto: update
Entity:
@Entity
public class Product {
@Id @GeneratedValue
private Long id;
private String name;
}
Repository:
public interface ProductRepository extends JpaRepository<Product, Long> {}
πΉ 8. What are records in Java 17 and how can they be used in Spring Boot?
β Answer:
Records are immutable data classes:
public record ProductDTO(Long id, String name) {}
Use in controller responses:
@GetMapping
public List<ProductDTO> getProducts() {
return List.of(new ProductDTO(1L, "Laptop"));
}
πΉ 9. How to validate a request in Spring Boot using Java Bean Validation?
β Example:
@RestController
public class UserController {
@PostMapping("/users")
public ResponseEntity<String> save(@Valid @RequestBody UserDto user) {
return ResponseEntity.ok("Valid");
}
}
class UserDto {
@NotBlank
private String name;
}
πΉ 10. How to secure Spring Boot REST API using Spring Security + JWT?
β Key Components:
UsernamePasswordAuthenticationFilter
JWT token utility
Custom UserDetailsService
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain security(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests()
.requestMatchers("/auth/**").permitAll()
.anyRequest().authenticated();
return http.build();
}
}
Empowering students through technology and creativity.
Support
Β© 2025. All rights reserved.