Spring Boot Interview Questions and Answers

Find 100+ Spring Boot interview questions and answers to assess candidates' skills in microservices, REST APIs, dependency injection, security, and performance optimization.
By
WeCP Team

As Spring Boot continues to be the backbone of modern Java-based microservices and enterprise applications in 2025, recruiters need to identify developers who can efficiently build, deploy, and manage scalable and high-performance applications. With its simplified configuration, built-in support for microservices, and seamless integration with cloud platforms, Spring Boot remains a top choice for backend development.

This resource, "100+ Spring Boot Interview Questions and Answers," is designed to help recruiters evaluate candidates effectively. It covers topics from fundamentals to advanced concepts, including Spring Boot starters, dependency injection, security, microservices architecture, and cloud deployment.

Whether hiring junior Java developers or experienced backend engineers, this guide enables you to assess a candidate’s:

  • Core Spring Boot Knowledge: Auto-configuration, annotations, and application properties.
  • Advanced Skills: RESTful API development, Spring Security, caching, and messaging with RabbitMQ/Kafka.
  • Real-World Proficiency: Performance tuning, testing with JUnit and Mockito, deploying on Docker, Kubernetes, and AWS.

For a streamlined assessment process, consider platforms like WeCP, which allow you to:

Create tailored Spring Boot assessments with hands-on coding challenges.
Include real-world scenarios to test API development, security, and database integration.
Conduct remote proctored exams to ensure test integrity.
Leverage AI-powered analysis for faster, data-driven hiring decisions.

Save time, improve hiring efficiency, and confidently recruit Spring Boot developers who can build robust, scalable applications from day one.

spring boot interview questionsEasy Level Questions (1-40)

  1. What is Spring Boot, and what are its main features?
  2. How does Spring Boot simplify the development of Java applications?
  3. What is the significance of the @SpringBootApplication annotation?
  4. How can you create a Spring Boot project using Spring Initializr?
  5. What are Spring Boot Starters?
  6. Explain the purpose of the application.properties or application.yml file.
  7. What is the default port on which a Spring Boot application runs?
  8. How do you create a simple RESTful API using Spring Boot?
  9. What is the @RestController annotation used for?
  10. How can you handle exceptions in a Spring Boot application?
  11. What is dependency injection, and how is it implemented in Spring Boot?
  12. Explain the role of the @Autowired annotation.
  13. How can you use the @Value annotation in Spring Boot?
  14. What is the difference between @Component, @Service, and @Repository?
  15. How do you run a Spring Boot application from the command line?
  16. What is the purpose of the @RequestMapping annotation?
  17. How do you define a bean in Spring Boot?
  18. What is Spring Boot DevTools?
  19. How can you include external libraries in a Spring Boot project?
  20. What are the default logging configurations in Spring Boot?
  21. How can you enable a Spring Boot application to use an embedded database?
  22. What is the significance of the @Entity annotation in Spring Boot?
  23. How do you configure a basic web application in Spring Boot?
  24. Explain the concept of profiles in Spring Boot.
  25. How can you access request parameters in a Spring Boot controller?
  26. What is the @ResponseBody annotation used for?
  27. How do you handle form data in Spring Boot?
  28. Explain the purpose of the @ComponentScan annotation.
  29. How can you customize the default error response in Spring Boot?
  30. What is the purpose of the @SpringBootTest annotation?
  31. How do you create a simple HTML page in a Spring Boot application?
  32. What are the types of dependency management available in Spring Boot?
  33. Explain how to use properties files for different environments in Spring Boot.
  34. What is the purpose of the @PathVariable annotation?
  35. How can you return a JSON response from a Spring Boot controller?
  36. How do you add custom filters in Spring Boot?
  37. What is the purpose of the @Scheduled annotation in Spring Boot?
  38. How can you enable CORS in a Spring Boot application?
  39. Explain how to configure multiple data sources in Spring Boot.
  40. What is the role of the @EnableAutoConfiguration annotation?

Intermediate Level Questions (1-40)

  1. What is Spring Boot’s auto-configuration, and how does it work?
  2. How do you create a RESTful web service using Spring Data JPA?
  3. Explain how to use Spring Boot with Thymeleaf.
  4. What is Actuator in Spring Boot, and what features does it provide?
  5. How can you implement security in a Spring Boot application?
  6. What is the purpose of the @Configuration annotation?
  7. How do you handle validations in Spring Boot?
  8. Explain the difference between @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping.
  9. How do you set up a MySQL database connection in Spring Boot?
  10. What is Spring Boot Starter Data JPA?
  11. How can you use Spring Boot with Redis?
  12. Explain how to implement pagination and sorting in Spring Data JPA.
  13. How do you create custom exception handlers in Spring Boot?
  14. What is the purpose of the @Transactional annotation?
  15. How do you use the @Scheduled annotation for background tasks?
  16. Explain the role of the @Profile annotation in Spring Boot.
  17. How can you customize logging in a Spring Boot application?
  18. What is Spring Boot Testing, and what annotations are commonly used?
  19. How do you handle HTTP headers in Spring Boot?
  20. Explain the use of @FeignClient for RESTful services in Spring Boot.
  21. What is the significance of the @EnableCaching annotation?
  22. How do you configure an embedded server in Spring Boot?
  23. What is Spring Boot's approach to configuration properties binding?
  24. How can you implement custom Spring Boot starters?
  25. Explain how to integrate Spring Boot with a messaging queue like RabbitMQ.
  26. What are the common security practices for Spring Boot applications?
  27. How do you perform unit testing in a Spring Boot application?
  28. How can you implement Swagger in a Spring Boot application?
  29. Explain how to monitor a Spring Boot application using Micrometer.
  30. How do you manage application secrets in Spring Boot?
  31. What is the purpose of the @ResponseStatus annotation?
  32. How can you handle multipart file uploads in Spring Boot?
  33. Explain the difference between synchronous and asynchronous processing in Spring Boot.
  34. How do you configure a REST API client in Spring Boot?
  35. What is the role of the @RequestBody annotation?
  36. How can you perform rate limiting in a Spring Boot application?
  37. Explain the use of HATEOAS in Spring Boot applications.
  38. How do you create a custom converter in Spring Boot?
  39. What are the best practices for handling transactions in Spring Boot?
  40. How do you use Spring Boot to implement an event-driven architecture?

Experience Level Questions (1-40)

  1. How do you design a microservices architecture using Spring Boot?
  2. Explain how to handle multi-module projects in Spring Boot.
  3. How can you integrate Spring Boot with Kubernetes?
  4. What are the best practices for managing application properties in Spring Boot?
  5. How do you implement API versioning in a Spring Boot application?
  6. Explain the purpose of the Spring Cloud ecosystem in conjunction with Spring Boot.
  7. How do you create custom metrics and health checks in Spring Boot?
  8. Describe the process of creating a custom starter in Spring Boot.
  9. What is the significance of reactive programming in Spring Boot?
  10. How can you implement OAuth2 authentication in a Spring Boot application?
  11. Explain how to configure distributed tracing in Spring Boot applications.
  12. What is the role of the Spring Boot Admin?
  13. How do you implement circuit breaker patterns in Spring Boot?
  14. What are the common challenges when deploying Spring Boot applications in production?
  15. How can you ensure high availability in a Spring Boot microservices architecture?
  16. Explain the use of Spring Cloud Config for externalized configuration.
  17. How do you manage service discovery in a Spring Boot application?
  18. What strategies can you use for monitoring and logging in production?
  19. How do you implement a retry mechanism in a Spring Boot application?
  20. What is the significance of the @EventListener annotation?
  21. Explain the role of Docker in deploying Spring Boot applications.
  22. How can you handle cross-service communication in Spring Boot?
  23. What are the benefits of using Spring WebFlux in Spring Boot?
  24. How do you implement server-sent events in a Spring Boot application?
  25. Explain the purpose of the @TransactionalEventListener annotation.
  26. How can you handle batch processing in a Spring Boot application?
  27. What are the considerations for securing microservices built with Spring Boot?
  28. Explain how to use Spring Boot with GraphQL.
  29. How do you implement asynchronous messaging with Kafka in Spring Boot?
  30. What is the role of Spring Security in protecting Spring Boot applications?
  31. How can you implement content negotiation in a Spring Boot application?
  32. Explain the difference between REST and SOAP in the context of Spring Boot.
  33. How do you perform load testing on a Spring Boot application?
  34. What are the best practices for exception handling in Spring Boot?
  35. How can you implement SSO (Single Sign-On) in Spring Boot applications?
  36. What is the significance of the @EnableAsync annotation?
  37. Explain how to use the Spring Boot testing framework for integration tests.
  38. How do you create a reactive RESTful service using Spring WebFlux?
  39. What are some common performance optimization techniques for Spring Boot applications?
  40. How can you implement feature toggles in a Spring Boot application?

Answer easy questions ;

1. What is Spring Boot, and what are its main features?

Spring Boot is a framework that simplifies the process of building production-ready Spring applications. It provides pre-configured settings, which eliminate much of the boilerplate code required by the traditional Spring framework. It also helps developers focus on writing business logic rather than worrying about configuration.

Main Features:

  • Auto-configuration: Automatically configures Spring applications based on the dependencies.
  • Starter POMs: Provides predefined dependencies for building different types of applications.
  • Embedded Servers: Comes with embedded servers like Tomcat, Jetty, or Undertow, eliminating the need for external server deployment.
  • Actuator: Provides endpoints for monitoring and managing applications.
  • Spring CLI: Enables developers to run and test Spring Boot applications using Groovy scripts.

2. How does Spring Boot simplify the development of Java applications?

Spring Boot simplifies Java application development by providing:

  • Auto-configuration: Automatically configures common Spring components based on classpath settings and beans. For instance, if a dependency like H2 or MySQL is present, it automatically configures a DataSource.
  • Embedded Servers: Built-in servers like Tomcat or Jetty eliminate the need for complex server setups.
  • Starter Dependencies: Pre-defined dependency sets (starters) reduce the complexity of adding dependencies manually.
  • Minimal XML Configuration: Almost everything is annotation-based, reducing the need for lengthy XML configuration files.

3. What is the significance of the @SpringBootApplication annotation?

The @SpringBootApplication annotation is a combination of three key annotations:

  • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration mechanism.
  • @ComponentScan: Scans for Spring components in the current package.
  • @Configuration: Marks the class as a source of bean definitions.

By using @SpringBootApplication, developers can replace multiple annotations with one, making the code cleaner and easier to manage.

4. How can you create a Spring Boot project using Spring Initializr?

You can create a Spring Boot project using Spring Initializr through the following steps:

  1. Visit the website start.spring.io.
  2. Select the Project (Maven or Gradle) and the Spring Boot Version.
  3. Fill in the group ID and artifact ID for your project.
  4. Select the dependencies like Spring Web, Spring Data JPA, or others.
  5. Click Generate, and it will download a zip file containing the project structure.
  6. Unzip the project and import it into your IDE.

5. What are Spring Boot Starters?

Spring Boot starters are pre-configured sets of dependencies that provide all the necessary libraries to start building specific types of applications. For example:

  • spring-boot-starter-web: For building web applications using Spring MVC and embedded Tomcat.
  • spring-boot-starter-data-jpa: For using Spring Data JPA with Hibernate.

These starters simplify dependency management and save developers from manually defining multiple dependencies.

6. Explain the purpose of the application.properties or application.yml file.

The application. properties or application. yml file is used to define configuration settings for a Spring Boot application. It contains key-value pairs for properties like server ports, database configurations, logging levels, and more.

Example of application. properties:

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root

Example of application.yml:

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: root

7. What is the default port on which a Spring Boot application runs?

By default, Spring Boot applications run on port 8080. This can be changed by setting the server.port property in the application.properties or application.yml file.

Example:

server.port=9090

8. How do you create a simple RESTful API using Spring Boot?

To create a simple RESTful API in Spring Boot:

  1. Add the spring-boot-starter-web dependency to your pom.xml or build.gradle.
  2. Use the @RestController annotation to define a REST controller.
  3. Define a method with the @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping annotations to handle HTTP requests.

Example:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

9. What is the @RestController annotation used for?

The @RestController annotation is used to define a RESTful web service controller. It combines the functionality of @Controller and @ResponseBody, meaning that methods in a class marked with @RestController will return data directly in the HTTP response body, typically as JSON or XML.

10. How can you handle exceptions in a Spring Boot application?

Exceptions can be handled globally in Spring Boot using:

  • @ExceptionHandler: To handle specific exceptions for a particular controller.
  • @ControllerAdvice: To define global exception handling across the application.

Example using @ControllerAdvice:

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Object> handleException(Exception e) {
        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

11. What is dependency injection, and how is it implemented in Spring Boot?


Dependency Injection
is a design pattern in which a class receives its dependencies from an external source rather than creating them internally. Spring Boot supports three types of dependency injection:

  • Constructor Injection: Preferred method where dependencies are passed via the constructor.
  • Setter Injection: Dependencies are set through setter methods.
  • Field Injection: Dependencies are injected directly into the fields.

Example:

@Service
public class MyService {
    private final MyRepository repository;
    
    @Autowired
    public MyService(MyRepository repository) {
        this.repository = repository;
    }
}

12. Explain the role of the @Autowired annotation.

The @Autowired annotation is used to inject dependencies automatically into a class by Spring’s Dependency Injection framework. It can be applied to constructors, setter methods, or fields. When used, Spring Boot will resolve and inject the required bean from the application context.

13. How can you use the @Value annotation in Spring Boot?

The @Value annotation is used to inject values from properties files into Spring beans. It helps in binding the value of configuration parameters to a field in a class.

Example:

@Component
public class MyComponent {
    @Value("${app.name}")
    private String appName;

    public String getAppName() {
        return appName;
    }
}

14. What is the difference between @Component, @Service, and @Repository?

  • @Component: A generic stereotype for any Spring-managed component.
  • @Service: A specialized @Component that is used to define service-layer beans.
  • @Repository: A specialization of @Component that marks the class as a Data Access Object (DAO), indicating that it deals with data persistence.

15. How do you run a Spring Boot application from the command line?

A Spring Boot application can be run from the command line using the following methods:

  • Using Maven:
    Run mvn spring-boot:run in the project directory.
  • Using a jar file:
    After building the application (e.g., using mvn package), run the jar file using the command java -jar target/yourapp.jar.

16. What is the purpose of the @RequestMapping annotation?

The @RequestMapping annotation is used to map HTTP requests to specific handler methods in a controller. It can be used at the class level to define a common base URL and at the method level to specify specific HTTP operations like GET, POST, PUT, DELETE, etc.

17. How do you define a bean in Spring Boot?

A bean can be defined in Spring Boot using the @Bean annotation within a @Configuration class or using stereotype annotations like @Component, @Service, or @Repository.

18. What is Spring Boot DevTools?

Spring Boot DevTools is a module that enhances the development experience by enabling automatic restarts, live reloading, and quick changes during development. It helps developers to see the effects of code changes without having to restart the application manually.

19. How can you include external libraries in a Spring Boot project?

You can include external libraries in a Spring Boot project by adding the library's dependency in the pom.xml (for Maven) or build.gradle (for Gradle).

Example in pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

20. What are the default logging configurations in Spring Boot?

Spring Boot uses Logback as the default logging framework. It auto-configures a console appender with different log levels (INFO, DEBUG, WARN, etc.). You can customize logging by changing properties in application.properties or application.yml.

21. How can you enable a Spring Boot application to use an embedded database?

Spring Boot can be configured to use an embedded database like H2, HSQLDB, or Derby. These databases are typically used during development and testing.

To enable an embedded database, follow these steps:

  1. Add the dependency for an embedded database in your pom.xml:
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
  1. Spring Boot automatically configures the embedded database when the application starts.

You can also define database properties in application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

22. What is the significance of the @Entity annotation in Spring Boot?

The @Entity annotation is used in JPA (Java Persistence API) to mark a class as a persistent entity, which means it will be mapped to a table in the database. Each instance of this class corresponds to a row in the table.

Example:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    // getters and setters
}

23. How do you configure a basic web application in Spring Boot?

To configure a basic web application in Spring Boot:

  1. Add the spring-boot-starter-web dependency in pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. Create a controller class using @RestController:
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}
  1. Run the application. The web server (e.g., Tomcat) will start on port 8080 by default, and the app can be accessed via http://localhost:8080/hello.

24. Explain the concept of profiles in Spring Boot.


Profiles in Spring Boot allow different configurations to be used for different environments (development, testing, production, etc.). You can define environment-specific configurations in separate properties files like application-dev.properties and application-prod.properties.

To activate a profile, use the spring.profiles.active property in application.properties or pass it as a JVM argument:

spring.profiles.active=dev

When active, Spring Boot will load configuration from application-dev.properties along with application.properties.

25. How can you access request parameters in a Spring Boot controller?

You can access request parameters in Spring Boot using the @RequestParam annotation in controller methods.

Example:

@RestController
public class GreetingController {
    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return "Hello, " + name;
    }
}

URL: http://localhost:8080/greet?name=Sanjeet
Output: Hello, Sanjeet

26. What is the @ResponseBody annotation used for?

The @ResponseBody annotation is used to indicate that the return value of a method should be written directly to the HTTP response body (rather than rendering a view). In Spring Boot, it is commonly used in RESTful services to return JSON or XML data.

Example:

@GetMapping("/user")
@ResponseBody
public User getUser() {
    return new User("Sanjeet", "sanjeet@example.com");
}

The method returns a JSON representation of the User object.

27. How do you handle form data in Spring Boot?

You can handle form data in Spring Boot using the @RequestParam or @ModelAttribute annotations.

Example using @RequestParam:

@PostMapping("/submit")
public String submitForm(@RequestParam String name, @RequestParam String email) {
    return "Form submitted by " + name + " with email " + email;
}

Example using @ModelAttribute:

@PostMapping("/submit")
public String submitForm(@ModelAttribute User user) {
    return "Form submitted by " + user.getName() + " with email " + user.getEmail();
}

28. Explain the purpose of the @ComponentScan annotation.

The @ComponentScan annotation is used to specify the packages to scan for Spring components like @Component, @Service, @Repository, and @Controller. By default, Spring Boot scans the package where the @SpringBootApplication annotated class resides, but @ComponentScan can be used to specify additional packages to scan.

29. How can you customize the default error response in Spring Boot?

To customize the default error response in Spring Boot, you can:

  1. Create a custom error controller by implementing the ErrorController interface.
  2. Customize the error view template or return a custom JSON response.

Example:

@Controller
public class CustomErrorController implements ErrorController {
    @RequestMapping("/error")
    public String handleError() {
        return "An error occurred!";
    }
}

Alternatively, you can create a custom JSON response by handling exceptions globally using @ControllerAdvice and @ExceptionHandler.

30. What is the purpose of the @SpringBootTest annotation?

The @SpringBootTest annotation is used to create an application context for integration testing in Spring Boot. It loads the entire application context, enabling tests to run with full application behavior, including dependency injection, autowiring, and property configurations.

Example:

@SpringBootTest
public class MyApplicationTests {
    @Test
    void contextLoads() {
        // Test context loading
    }
}

31. How do you create a simple HTML page in a Spring Boot application?

To create a simple HTML page in a Spring Boot application:

  1. Add the spring-boot-starter-thymeleaf dependency to your pom.xml.
  2. Create an HTML file in the src/main/resources/templates/ folder.

Example HTML (src/main/resources/templates/hello.html):

<html>
<body>
    <h1>Hello, Thymeleaf!</h1>
</body>
</html>

In the controller, use @GetMapping to return the view:

@Controller
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

32. What are the types of dependency management available in Spring Boot?

Spring Boot provides dependency management through:

  1. Starter dependencies: Predefined dependency bundles for common use cases (e.g., spring-boot-starter-web, spring-boot-starter-data-jpa).
  2. Custom dependencies: You can define custom versions of dependencies in the pom.xml or build.gradle.
  3. Version management: Spring Boot manages dependency versions to ensure compatibility among libraries.

33. Explain how to use properties files for different environments in Spring Boot.

Spring Boot allows you to create environment-specific configuration files like application-dev.properties or application-prod.properties. You can specify which file to use by setting the spring.profiles.active property.

Example:

spring.profiles.active=dev

34. What is the purpose of the @PathVariable annotation?

The @PathVariable annotation is used to extract values from the URI path. It is often used in RESTful services to capture dynamic parts of the URL.

Example:

@GetMapping("/user/{id}")
public String getUser(@PathVariable("id") Long id) {
    return "User ID: " + id;
}

35. How can you return a JSON response from a Spring Boot controller?

To return a JSON response from a Spring Boot controller, ensure that the controller method is annotated with @ResponseBody or use @RestController for the entire class. Spring Boot automatically converts the returned object to JSON using Jackson.

Example:

@RestController
public class UserController {
    @GetMapping("/user")
    public User getUser() {
        return new User("Sanjeet", "sanjeet@example.com");
    }
}

36. How can you configure logging levels in Spring Boot?

Logging levels in Spring Boot can be configured in the application.properties or application.yml file.

Example:

logging.level.org.springframework=INFO
logging.level.com.example=DEBUG

37. What is Spring Boot Actuator?

Spring Boot Actuator provides production-ready features like monitoring, metrics, health checks, and environment information via HTTP endpoints. Actuator endpoints include /health, /metrics, /info, and more.

To enable Actuator, add the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

38. How can you disable a specific Spring Boot auto-configuration?

You can disable a specific Spring Boot auto-configuration by using the exclude attribute in the @SpringBootApplication annotation:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyApplication {
}

39. What is @EnableAutoConfiguration used for in Spring Boot?

The @EnableAutoConfiguration annotation in Spring Boot enables the framework to configure Spring Beans automatically based on the dependencies and classpath settings. It is typically used as part of the @SpringBootApplication annotation.

40. How do you configure a custom banner in Spring Boot?

To configure a custom banner, create a banner.txt file in the src/main/resources directory and put your custom ASCII art or text inside it. Spring Boot will display the contents of this file in the console when the application starts.

Intermediate level answer

1. What is Spring Boot Actuator, and what are its key features?

Spring Boot Actuator provides production-ready features to help monitor and manage Spring Boot applications. Key features include:

  • Health Check: Provides the current health status of the application.
  • Metrics: Collects application metrics like memory usage, CPU load, and more.
  • Auditing: Tracks security-related events in the application.
  • Environment Information: Shows details about the environment such as configuration properties, system properties, etc.
  • Custom Endpoints: You can create custom Actuator endpoints to expose application-specific information.

You can access these endpoints like /actuator/health, /actuator/metrics, and more.

2. How can you customize Actuator endpoints in Spring Boot?

You can customize Actuator endpoints in Spring Boot using application.properties:

Change Base Path:

management.endpoints.web.base-path=/manage

Enable or Disable Specific Endpoints:

management.endpoint.health.enabled=true
management.endpoint.shutdown.enabled=true

Additionally, custom endpoints can be created using the @Endpoint or @RestController annotations.

3. What are the main differences between @RestController and @Controller in Spring Boot?

  • @RestController: A combination of @Controller and @ResponseBody. It returns JSON/XML directly in the response body, typically used for RESTful web services.
  • @Controller: Used in MVC applications where the view (like HTML or Thymeleaf templates) is returned. Methods need to be annotated with @ResponseBody if you want to return JSON/XML.

4. How does Spring Boot support externalized configuration?

Spring Boot allows you to externalize configuration via properties files (application.properties or application.yml), environment variables, and command-line arguments. This helps in separating application code from configuration, enabling easy changes across different environments (dev, prod).

You can access configuration values in Spring Boot using:

@Value("${property.name}")
private String propertyName;

5. What is the purpose of @ConfigurationProperties in Spring Boot?

The @ConfigurationProperties annotation is used to map properties from the application.properties or application.yml files to POJO classes, allowing type-safe access to configuration values.

Example:

@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private String name;
    private int version;
    // getters and setters
}

Then, add @EnableConfigurationProperties(AppConfig.class) in the main application class.

6. How can you secure a Spring Boot application?

Spring Boot can be secured using Spring Security. To enable it:

  1. Add the spring-boot-starter-security dependency.
  2. Define security rules using a configuration class extending WebSecurityConfigurerAdapter.

Example:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
            .and().formLogin();
    }
}

7. What is Spring Data JPA, and how is it integrated into Spring Boot?

Spring Data JPA is a module of Spring that provides a data access layer based on JPA (Java Persistence API). It simplifies database access by providing repository interfaces with built-in CRUD operations.

To integrate it into Spring Boot:

  1. Add the spring-boot-starter-data-jpa dependency.
  2. Create a repository interface that extends JpaRepository.
  3. Spring Boot auto-configures the JPA entity manager and transaction management.

Example:

public interface UserRepository extends JpaRepository<User, Long> {}

8. How does Spring Boot handle database initialization and schema generation?

Spring Boot can automatically initialize the database and generate the schema by leveraging JPA and Hibernate. You can configure this behavior in the application.properties file:

spring.jpa.hibernate.ddl-auto=create-drop

Other options include none, validate, update, and create.

For running SQL scripts, you can place schema.sql or data.sql files in src/main/resources, and Spring Boot will automatically execute them on application startup.

9. What is Spring Boot’s auto-configuration feature?

Spring Boot’s auto-configuration feature automatically configures beans and settings based on the dependencies and the environment. It tries to guess the best configuration by inspecting the classpath and properties.

Auto-configuration can be disabled globally or selectively:

Globally using:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
  • Selectively for certain beans using @ConditionalOnMissingBean or @ConditionalOnClass.

10. What are some strategies for managing transactions in Spring Boot?
Spring Boot manages transactions using Spring’s @Transactional annotation. Some strategies include:

  • Declarative Transaction Management: Use @Transactional to demarcate transaction boundaries in service methods.
  • Programmatic Transaction Management: Use PlatformTransactionManager to manage transactions programmatically.
  • Rollback Strategies: Specify rollback conditions using @Transactional(rollbackFor = Exception.class).

11. How does Spring Boot handle dependency injection?
Spring Boot handles dependency injection using the core principles of Inversion of Control (IoC) in Spring. It automatically injects beans into your components using @Autowired (field, constructor, or setter injection).

Example:

@Service
public class UserService {
    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

12. What is a Spring Boot starter, and how does it simplify dependency management?

Spring Boot starters are pre-configured sets of dependencies that help developers quickly include the needed libraries for a specific task without manually managing individual dependencies.

Common examples include:

  • spring-boot-starter-web: For building web applications with Spring MVC.
  • spring-boot-starter-data-jpa: For database access using Spring Data JPA.
  • spring-boot-starter-security: For adding Spring Security.

13. Explain the role of @SpringBootApplication annotation in Spring Boot.

The @SpringBootApplication annotation is a convenience annotation that combines three annotations:

  • @Configuration: Marks the class as a source of bean definitions.
  • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration feature.
  • @ComponentScan: Enables component scanning to detect Spring-managed components like @Controller, @Service, @Repository, etc.

14. How does Spring Boot handle asynchronous processing?

Spring Boot supports asynchronous processing using the @Async annotation. By marking a method with @Async, it is executed in a separate thread asynchronously.

Example:

@Service
public class AsyncService {
    @Async
    public void asyncMethod() {
        // some asynchronous logic
    }
}

To enable asynchronous processing, you need to annotate your configuration class with @EnableAsync.

15. What are Spring Boot Profiles, and how are they used?

Profiles in Spring Boot allow the application to load different configurations based on the active environment. For example, you can have separate properties files for development and production environments (application-dev.properties, application-prod.properties).

To activate a profile, use the spring.profiles.active property:

spring.profiles.active=dev

16. How do you handle exceptions globally in a Spring Boot application?

You can handle exceptions globally using the @ControllerAdvice and @ExceptionHandler annotations.

Example:

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

17. What is the default embedded server used in Spring Boot, and how can you change it?

The default embedded server used in Spring Boot is Tomcat. You can change it by excluding the Tomcat dependency and including a different server like Jetty or Undertow in your pom.xml.

Example (for Jetty):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

18. How can you monitor Spring Boot application metrics using Actuator?

Actuator provides the /actuator/metrics endpoint that exposes a variety of application metrics like memory usage, request counts, and more. You can enable it in application.properties:

management.endpoint.metrics.enabled=true

You can also monitor specific metrics like JVM memory using:

GET /actuator/metrics/jvm.memory.used

19. What are Spring Boot starters, and how do they improve dependency management?

Spring Boot starters are a set of pre-defined dependency packages that simplify adding necessary libraries for specific functionalities. They encapsulate dependencies and ensure compatibility, saving developers from manually including and managing multiple dependencies.

Common examples:

  • spring-boot-starter-web: Adds dependencies for building a web application.
  • spring-boot-starter-data-jpa: Adds Spring Data JPA and Hibernate dependencies.

20. What is the use of @ConditionalOnMissingBean in Spring Boot?

The @ConditionalOnMissingBean annotation is used to conditionally load a bean only if the specified bean is not already present in the Spring context. This is often used in auto-configuration classes to provide default beans that can be overridden.

Example:

@Bean
@ConditionalOnMissingBean
public DataSource dataSource() {
    return new HikariDataSource();
}

21. How does Spring Boot’s devtools help during development?

Spring Boot DevTools offers a set of tools that improve developer productivity. Key features include:

  • Automatic Restart: Automatically restarts the application when files change.
  • LiveReload: Automatically refreshes the browser when resources change.
  • Disabling Caching: Helps developers see changes immediately without caching static resources.

To use devtools, add the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

22. Explain how to test a Spring Boot application.

Spring Boot provides built-in support for testing through:

  • Unit Testing: Using JUnit and Mockito for testing individual components.
  • Integration Testing: Using @SpringBootTest to load the full application context and test the interaction between different components.

Example of unit testing with MockMVC for testing controllers:

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldReturnUser() throws Exception {
        mockMvc.perform(get("/user"))
               .andExpect(status().isOk())
               .andExpect(content().json("{name: 'Sanjeet'}"));
    }
}

23. What is Spring Boot’s @DataJpaTest used for?

@DataJpaTest is used for testing JPA repositories. It provides configuration that is limited to testing JPA components without the need to load the full application context.

Example:

@DataJpaTest
public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindByName() {
        User user = userRepository.findByName("Sanjeet");
        assertNotNull(user);
    }
}

24. How do you configure multiple data sources in Spring Boot?

Configuring multiple data sources in Spring Boot involves:

  1. Defining multiple DataSource beans.
  2. Specifying which DataSource is primary.
  3. Configuring multiple EntityManager and TransactionManager beans.

Example configuration:

@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "secondaryDataSource")
@Configuration

25. What is the purpose of @EnableScheduling in Spring Boot?

The @EnableScheduling annotation is used in Spring Boot to enable support for scheduled tasks. It allows methods to be executed at fixed intervals or with custom schedules by using the @Scheduled annotation on methods.

Example:

@EnableScheduling
@SpringBootApplication
public class MyApplication {}

@Component
public class ScheduledTask {
    @Scheduled(fixedRate = 5000)
    public void run() {
        System.out.println("Task is running every 5 seconds");
    }
}

26. How do you implement pagination and sorting in Spring Boot using Spring Data JPA?

Pagination and sorting in Spring Data JPA are achieved using the Pageable and Sort interfaces. The repository method should accept a Pageable parameter for pagination and a Sort parameter for sorting.

Example:

public interface UserRepository extends JpaRepository<User, Long> {
    Page<User> findByLastName(String lastName, Pageable pageable);
}

Pageable pageable = PageRequest.of(0, 10, Sort.by("firstName"));
Page<User> users = userRepository.findByLastName("Doe", pageable);

27. What is the @EnableAutoConfiguration annotation used for, and how can you exclude certain configurations?

The @EnableAutoConfiguration annotation in Spring Boot enables automatic configuration based on the classpath settings and environment. It allows Spring Boot to automatically configure beans and settings needed for the application.

To exclude specific auto-configurations, use the exclude attribute:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication {}

28. How do you configure an embedded in-memory database like H2 in Spring Boot?

Spring Boot can auto-configure H2 as an in-memory database if the H2 dependency is on the classpath. You can configure it in application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true

This configuration starts an H2 in-memory database and enables the H2 console at /h2-console.

29. What is the use of the @Profile annotation in Spring Boot?

The @Profile annotation in Spring Boot is used to specify beans that should be created only when a specific profile is active. It helps in defining different beans for different environments (e.g., dev, prod).

Example:

@Bean
@Profile("dev")
public DataSource devDataSource() {
    return new HikariDataSource();
}

@Bean
@Profile("prod")
public DataSource prodDataSource() {
    return new HikariDataSource();
}

30. What are the different ways of running Spring Boot applications?

There are several ways to run a Spring Boot application:

Using java -jar: You can package the application as a JAR file and run it using the java -jar command.

java -jar myapp.jar

Using Maven/Gradle: Run the application with Maven or Gradle:

mvn spring-boot:run
  • Using an IDE: You can run the main() method of the application directly from an IDE like IntelliJ IDEA or Eclipse.

31. How can you access a property value defined in application.properties in your Java code?
You can access property values defined in application.properties using the @Value annotation or by using @ConfigurationProperties.

Example using @Value:

@Value("${app.name}")
private String appName;

Example using @ConfigurationProperties:

@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private String name;
    // getters and setters
}

32. What is the use of the spring-boot-starter-test dependency in Spring Boot?

The spring-boot-starter-test dependency provides essential libraries and tools for testing Spring Boot applications. It includes:

  • JUnit 5: For unit and integration testing.
  • Mockito: For mocking objects during tests.
  • Spring Test: For integration testing.
  • AssertJ: For fluent assertions.

33. How can you perform file upload in Spring Boot?

Spring Boot allows file uploads using MultipartFile. To enable file uploads, ensure that the multipart feature is enabled in application.properties:

spring.servlet.multipart.enabled=true

Example of file upload controller:

@PostMapping("/upload")
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
    // process file
    return "File uploaded successfully!";
}

34. What is Spring Boot’s default logging framework? How can you change the logging level?

Spring Boot uses Logback as the default logging framework. The logging level can be configured in application.properties:

logging.level.org.springframework=INFO
logging.level.com.example=DEBUG

You can change the default logging framework by excluding Logback and including dependencies for Log4j or another logging framework.

35. What is the purpose of the @Conditional annotation in Spring Boot?

The @Conditional annotation is used to define beans or configurations conditionally, based on certain conditions. Spring provides several conditional annotations, like @ConditionalOnProperty, @ConditionalOnMissingBean, etc.

Example:

@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public FeatureService featureService() {
    return new FeatureService();
}

36. How do you handle cross-origin requests (CORS) in Spring Boot?

Cross-Origin Resource Sharing (CORS) can be enabled in Spring Boot by using the @CrossOrigin annotation or by configuring it globally through a WebMvcConfigurer.

Example with @CrossOrigin:

@CrossOrigin(origins = "http://example.com")
@GetMapping("/resource")
public ResponseEntity<String> getResource() {
    return ResponseEntity.ok("Resource data");
}

Global configuration using WebMvcConfigurer:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("http://example.com");
    }
}

37. What is a @RestControllerAdvice in Spring Boot, and how is it used?

@RestControllerAdvice is used to handle exceptions globally in Spring Boot applications for REST controllers. It combines @ControllerAdvice and @ResponseBody.

Example:

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

38. What is the use of @Entity in Spring Boot?

The @Entity annotation is used in Spring Boot to mark a class as a JPA entity. The entity is mapped to a database table, and its fields are mapped to columns in that table.

Example:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
}

39. How do you implement caching in Spring Boot?

Caching in Spring Boot can be implemented using Spring’s caching abstraction. First, enable caching by adding @EnableCaching in your configuration class. Then, use @Cacheable, @CachePut, and @CacheEvict annotations on methods to cache their results.

Example:

@Service
public class UserService {
    @Cacheable("users")
    public User findUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }
}

40. How can you customize error handling in Spring Boot?

Spring Boot provides default error handling through /error endpoint. To customize error handling, you can:

  • Create a custom error page by creating an error.html page in src/main/resources/templates.
  • Implement a @ControllerAdvice class with @ExceptionHandler methods to handle specific exceptions.

Experience level answers:

1. How do you configure and handle security in Spring Boot applications?

Spring Boot integrates with Spring Security to handle authentication and authorization. You can secure your application using spring-boot-starter-security, which provides built-in security mechanisms such as HTTP Basic, form-based login, and CSRF protection.

For custom security configurations, you create a class that extends WebSecurityConfigurerAdapter and override methods to configure HTTP security.

Example:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/public/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .and()
            .logout().permitAll();
    }
}
  • You can further configure OAuth2 or JWT for token-based authentication in more complex applications.
  • Spring Security also allows method-level security using @Secured or @PreAuthorize.

2. How can you implement distributed tracing in Spring Boot microservices?


Distributed tracing helps track requests that span multiple microservices. You can implement distributed tracing in Spring Boot applications using Spring Cloud Sleuth along with Zipkin or Jaeger as the tracing systems.Steps to set up:

  1. Add the spring-cloud-starter-sleuth and spring-cloud-sleuth-zipkin dependencies.
  2. Sleuth automatically adds trace and span IDs to requests and logs to help trace the flow of a request across different microservices.

Example in application.properties:

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0

This configuration sends traces to Zipkin, allowing you to visualize how requests move through the system.

3. How can you achieve high availability and fault tolerance in Spring Boot applications?

High availability and fault tolerance in Spring Boot applications can be achieved using various approaches:

  • Service Discovery: Use Spring Cloud with Eureka for service registration and discovery, which enables load balancing and high availability.
  • Circuit Breaker: Integrate with Resilience4j or Hystrix to implement circuit breakers, fallback mechanisms, and rate limiting.

Example of circuit breaker using Resilience4j:

@RestController
public class MyController {
    @GetMapping("/data")
    @CircuitBreaker(name = "backendA", fallbackMethod = "fallback")
    public String getData() {
        // call to external service
    }
    
    public String fallback(Throwable t) {
        return "Fallback response";
    }
}
  • Load Balancing: Use Spring Cloud LoadBalancer or Ribbon to distribute requests across multiple instances of microservices.
  • Retry Mechanism: Retry failed operations using Resilience4j retry functionality or Spring’s @Retryable annotation.

4. What is the role of Spring Boot Admin, and how do you set it up?


Spring Boot Admin
is used to monitor and manage Spring Boot applications in real-time. It provides a dashboard where you can view the status of different services, monitor metrics, view logs, and more.

To set up Spring Boot Admin:

Add the following dependency to your admin server:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

1.Add the admin client dependency to the application you want to monitor:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>

2.Configure application.properties:

spring.boot.admin.client.url=http://localhost:8080

3.The admin server aggregates and displays the data of the registered applications.

5. Explain how Spring Boot handles reactive programming with WebFlux.


Spring Boot supports reactive programming using Spring WebFlux. Reactive programming is an asynchronous, non-blocking programming model that handles high-concurrency environments efficiently. WebFlux is based on the Reactor framework and supports reactive types like Mono and Flux.

Example of a reactive controller using Mono and Flux:

@RestController
public class ReactiveController {
    @GetMapping("/mono")
    public Mono<String> getMono() {
        return Mono.just("Hello Mono");
    }
    
    @GetMapping("/flux")
    public Flux<String> getFlux() {
        return Flux.just("Hello", "Flux");
    }
}

WebFlux works well with backpressure mechanisms to handle data streams efficiently, making it suitable for high-load applications such as chat apps or real-time systems.

6. How can you optimize the performance of Spring Boot applications?

Performance optimization in Spring Boot involves several strategies:

  • Use of Caching: Use Spring’s caching abstraction (@Cacheable, @CachePut) to cache frequently used data.

Connection Pooling: Configure a connection pool (e.g., HikariCP) for efficient database access. Example:

spring.datasource.hikari.maximum-pool-size=10

Lazy Initialization: Enable lazy initialization to load beans only when they are needed.

spring.main.lazy-initialization=true
  • Optimize Database Queries: Use pagination, query optimizations, and indexing for large datasets.
  • Use Asynchronous Methods: Annotate methods with @Async to perform tasks asynchronously.

7. What are the challenges of deploying Spring Boot applications in a containerized environment (e.g., Docker)?


Common challenges when deploying Spring Boot applications in Docker include:

  • Managing Environment Variables: You need to externalize configurations using environment variables or configuration management tools like Spring Cloud Config.
  • Container Scalability: Scaling containerized Spring Boot applications requires proper orchestration using Kubernetes or Docker Swarm.
  • Handling State: Spring Boot applications should be designed stateless for better scalability and fault tolerance. Any state should be stored in external services (e.g., databases or distributed caches).
  • Health Checks: Configure liveness and readiness probes for monitoring container health.

Example Dockerfile for a Spring Boot application:

FROM openjdk:11-jre
COPY target/app.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

8. How do you ensure the reliability and availability of Spring Boot microservices in production?

Ensuring reliability and availability in Spring Boot microservices involves:

  • Load Balancing: Use service discovery and client-side load balancing (e.g., Eureka + Ribbon or Spring Cloud LoadBalancer).
  • Circuit Breakers: Implement circuit breakers to prevent cascading failures in a distributed system.
  • Health Checks and Monitoring: Use Spring Boot Actuator to expose health check endpoints and integrate with monitoring tools (e.g., Prometheus, Grafana).
  • Resilience Strategies: Use retry mechanisms, timeouts, and bulkheads to handle transient failures.
  • Horizontal Scaling: Scale microservices horizontally using containers (Docker) and orchestration platforms (Kubernetes).

9. How do you handle database migrations in Spring Boot applications?

Spring Boot supports database migrations using tools like Flyway and Liquibase.

  • Flyway: It tracks database schema changes and applies migrations automatically during startup. You define migration scripts in the src/main/resources/db/migration folder. Example:
V1__initial_schema.sql
V2__add_new_column.sql

To enable Flyway, add the dependency:

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>
  • Liquibase: It offers more control over migrations, with support for complex refactorings. You define changes in an XML, YAML, or JSON file, known as a changelog.

10. What are some best practices for error handling in Spring Boot applications?


Best practices for error handling in Spring Boot applications include:

  • Global Exception Handling: Use @ControllerAdvice to handle exceptions globally and return consistent error responses.
  • Custom Error Responses: Define a custom error response class to structure error messages, including error codes and user-friendly messages.
  • HTTP Status Codes: Return appropriate HTTP status codes for different types of errors (e.g., 404 for Not Found, 500 for Server Error).
  • Logging: Log errors for debugging purposes and monitor application health.
  • Documentation: Document error codes and messages for API consumers.

Example of a global exception handler:

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
        ErrorResponse errorResponse = new ErrorResponse("RESOURCE_NOT_FOUND", ex.getMessage());
        return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
    }
}

11. How do you implement logging in Spring Boot applications?

Spring Boot uses SLF4J with Logback as the default logging framework. You can configure logging properties in the application.properties or application.yml file.

Example of logging configuration:

logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log

To use logging in your application, you inject a logger instance into your class:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
    
    public void doSomething() {
        logger.info("Doing something");
    }
}

For structured logging, consider using Logstash or ELK Stack for centralized logging and monitoring.

12. What is the Spring Boot DevTools, and how does it improve the development experience?

Spring Boot DevTools is a set of tools that enhance the development experience by providing features like automatic restarts, live reload, and enhanced logging. It helps developers see changes in their code without needing to restart the application manually.

To enable DevTools, add the following dependency in your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

Key features:

  • Automatic Restart: The application restarts automatically when files on the classpath change.
  • Live Reload: Automatically refreshes the browser when static resources change.
  • Enhanced Logging: Provides additional logging capabilities during development.

Note: DevTools should not be included in the production environment.

13. How can you configure custom error pages in Spring Boot?

You can configure custom error pages in Spring Boot by creating HTML or JSP files in the src/main/resources/templates directory. By default, Spring Boot will map error codes to specific error pages.

For example, create an error.html for generic errors and 404.html for 404 errors:

<!-- src/main/resources/templates/error.html -->
<html>
<body>
    <h1>Error</h1>
    <p>Something went wrong.</p>
</body>
</html>

To create specific error pages, you can use the following naming conventions:

  • error.html for generic error
  • 404.html for Not Found errors
  • 500.html for Internal Server errors

You can also customize error handling by implementing ErrorController:

@Controller
public class CustomErrorController implements ErrorController {
    @RequestMapping("/error")
    public String handleError() {
        return "error"; // return the name of the error page
    }
}

14. What are the differences between Spring Boot and traditional Spring Framework?

  • Configuration: Spring Boot provides convention over configuration, significantly reducing the amount of configuration needed compared to traditional Spring. It uses auto-configuration to automatically set up beans based on the classpath.
  • Standalone Applications: Spring Boot applications are standalone and can be run with a simple java -jar command, whereas traditional Spring applications typically require a web server like Tomcat or Jetty.
  • Embedded Servers: Spring Boot comes with embedded servers (like Tomcat, Jetty) that can be bundled with the application, while traditional Spring requires deploying to an external server.
  • Production-Ready Features: Spring Boot includes built-in features like health checks, metrics, and externalized configuration, which need to be manually added in traditional Spring applications.

15. How do you handle file uploads in Spring Boot applications?

To handle file uploads in Spring Boot, you can use @RequestParam to receive multipart files. Ensure you have the spring-boot-starter-web dependency in your project.

Example of a file upload controller:

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // Save the file to the server
            Path path = Paths.get("uploads/" + file.getOriginalFilename());
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
            return ResponseEntity.ok("File uploaded successfully: " + file.getOriginalFilename());
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("File upload failed");
        }
    }
}

In your HTML form, ensure you use enctype="multipart/form-data":

<form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button type="submit">Upload</button>
</form>

16. How do you enable CORS in a Spring Boot application?

CORS (Cross-Origin Resource Sharing) can be enabled in Spring Boot by configuring a CorsConfigurationSource bean or using the @CrossOrigin annotation at the controller or method level.

Example of global CORS configuration:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://example.com")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*");
    }
}

Example of using @CrossOrigin:

@RestController
@CrossOrigin(origins = "http://example.com")
public class MyController {
    @GetMapping("/data")
    public ResponseEntity<String> getData() {
        return ResponseEntity.ok("Data");
    }
}

17. What is the purpose of the @SpringBootApplication annotation?

The @SpringBootApplication annotation is a convenience annotation that combines several other annotations:

  • @Configuration: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.
  • @EnableAutoConfiguration: Enables Spring Boot's auto-configuration mechanism, which automatically configures beans based on the dependencies present in the classpath.
  • @ComponentScan: Enables component scanning, allowing Spring to find and register beans in the specified package.

It serves as the main entry point for Spring Boot applications and is typically used on the main application class.

Example:

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

18. How can you integrate Spring Boot with a NoSQL database like MongoDB?

To integrate Spring Boot with MongoDB, you need the spring-boot-starter-data-mongodb dependency. This provides support for MongoDB repository and template.

Example of integration:

  1. Add the dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
  1. Configure connection properties in application.properties:
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase
  1. Create a model class:
@Document(collection = "users")
public class User {
    @Id
    private String id;
    private String name;
    // Getters and setters
}
  1. Create a repository interface:
public interface UserRepository extends MongoRepository<User, String> {
    List<User> findByName(String name);
}
  1. Use the repository in your service or controller.

19. What are Spring Boot starters, and how do they simplify dependency management?

Spring Boot starters are a set of convenient dependency descriptors that aggregate common libraries and dependencies for specific functionalities. They simplify dependency management by allowing developers to include a single starter dependency instead of managing multiple individual dependencies.

For example:

  • spring-boot-starter-web includes dependencies for Spring MVC, Tomcat, and Jackson for JSON handling.
  • spring-boot-starter-data-jpa includes Spring Data JPA and Hibernate.

Example of using a starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

By using starters, you can quickly set up a Spring Boot application with all the necessary dependencies for specific features.

20. How do you use Swagger for API documentation in Spring Boot?

Swagger is used for API documentation and can be easily integrated into Spring Boot applications using springdoc-openapi or springfox-swagger2.

Example using springdoc-openapi:

  1. Add the dependency:
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.10</version>
</dependency>
  1. You can access the API documentation at /swagger-ui.html once the application is running.

Example of annotating a controller:

@RestController
@RequestMapping("/api/users")
public class UserController {
    @GetMapping
    @Operation(summary = "Get all users")
    public List<User> getAllUsers() {
        return userService.findAll();
    }
}

You can also customize API information using the @OpenAPIDefinition annotation.

21. What are the advantages of using Spring Boot over other frameworks?

Advantages of using Spring Boot include:

  • Rapid Development: Spring Boot reduces development time with its auto-configuration and embedded servers, allowing for faster project setup.
  • Convention Over Configuration: It provides sensible defaults and minimizes the need for manual configuration.
  • Microservices Support: It has built-in support for building microservices, including features like Spring Cloud for distributed systems.
  • Production-Ready Features: It offers production-ready features like monitoring, health checks, and externalized configuration.
  • Community and Ecosystem: A vast community and rich ecosystem of libraries and tools make it easy to find solutions and support.

22. How can you handle exceptions in REST APIs using Spring Boot?

Exception handling in REST APIs can be achieved using @ControllerAdvice and @ExceptionHandler to manage exceptions globally.

Example of a global exception handler:

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
        ErrorResponse errorResponse = new ErrorResponse("NOT_FOUND", ex.getMessage());
        return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex) {
        ErrorResponse errorResponse = new ErrorResponse("INTERNAL_SERVER_ERROR", "An error occurred");
        return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

This approach allows you to return structured error responses for different exception types.

23. What is the use of @Async annotation in Spring Boot?

The @Async annotation in Spring Boot is used to run methods asynchronously in a separate thread, allowing the main thread to continue without waiting for the asynchronous task to complete. This is useful for long-running operations.

To enable asynchronous processing, add @EnableAsync to your configuration class:

@Configuration
@EnableAsync
public class AsyncConfig {
}

Example of using @Async:

@Service
public class MyService {
    @Async
    public void performAsyncTask() {
        // Long-running task
    }
}

You can also specify a Future return type if you need to handle the result of the asynchronous method.

24. How do you implement pagination and sorting in Spring Data JPA?

Spring Data JPA provides built-in support for pagination and sorting through Pageable and Sort interfaces.

Example of using pagination and sorting:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    Page<User> findByName(String name, Pageable pageable);
}

In your service or controller, you can create PageRequest objects to define pagination and sorting:

@GetMapping("/users")
public Page<User> getUsers(@RequestParam int page, @RequestParam int size, @RequestParam String sort) {
    Pageable pageable = PageRequest.of(page, size, Sort.by(sort));
    return userRepository.findByName("John", pageable);
}

This allows you to retrieve a subset of results with sorting capabilities.

25. What is Spring Cloud, and how does it relate to Spring Boot?

Spring Cloud is a set of tools and frameworks designed to help developers build distributed systems and microservices architectures. It provides features like configuration management, service discovery, circuit breakers, and more.

Spring Boot serves as the foundation for Spring Cloud applications, providing the necessary tools to build stand-alone, production-ready Spring applications. By combining Spring Boot with Spring Cloud, developers can leverage auto-configuration and starter dependencies to simplify the setup and configuration of cloud-native applications.

Examples of Spring Cloud components:

  • Spring Cloud Config: For externalized configuration management.
  • Spring Cloud Netflix: For service discovery (Eureka), circuit breakers (Hystrix), and API gateway (Zuul).
  • Spring Cloud Gateway: For routing and filtering requests in microservices.

26. How do you configure a datasource in Spring Boot?

Configuring a datasource in Spring Boot is done through properties in application.properties or application.yml.

Example of configuring a MySQL datasource:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

If you are using JPA, you can also include:

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

With these configurations, Spring Boot will automatically create a DataSource bean, and you can use repositories or EntityManager to interact with the database.

27. What are Spring Boot CommandLineRunner and ApplicationRunner interfaces?

CommandLineRunner and ApplicationRunner are two interfaces provided by Spring Boot that can be implemented to execute code after the application context has been loaded.

  • CommandLineRunner: The run(String... args) method allows you to access the command-line arguments passed to the application.
  • ApplicationRunner: The run(ApplicationArguments args) method provides a more detailed view of the command-line arguments.

Example using CommandLineRunner:

@Component
public class StartupRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Application started with command-line arguments: " + Arrays.toString(args));
    }
}

Example using ApplicationRunner:

@Component
public class StartupRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Application started with option names: " + args.getOptionNames());
    }
}

28. How can you create a REST API with Spring Boot using JPA?

To create a REST API with Spring Boot using JPA, follow these steps:

  1. Set up a Spring Boot project with the required dependencies (Spring Web, Spring Data JPA, and a database driver).
  2. Create an entity class to represent the data model.
  3. Create a repository interface that extends JpaRepository.
  4. Create a service layer to handle business logic.
  5. Create a controller class to handle HTTP requests.

Example of a REST API:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    // Getters and Setters
}

public interface UserRepository extends JpaRepository<User, Long> {}

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public User createUser(User user) {
        return userRepository.save(user);
    }
}

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}

29. How can you use Spring Boot with external REST APIs?

To use Spring Boot with external REST APIs, you can leverage RestTemplate or the WebClient class (for reactive programming).

Example using RestTemplate:

  1. Add the dependency for spring-boot-starter-web.
  2. Create a RestTemplate bean:
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
  1. Use the RestTemplate to make API calls:
@Service
public class ApiService {
    @Autowired
    private RestTemplate restTemplate;

    public String getDataFromExternalApi() {
        ResponseEntity<String> response = restTemplate.getForEntity("https://api.example.com/data", String.class);
        return response.getBody();
    }
}

Example using WebClient (for reactive programming):

@Bean
public WebClient.Builder webClientBuilder() {
    return WebClient.builder();
}

@Service
public class ApiService {
    @Autowired
    private WebClient.Builder webClientBuilder;

    public Mono<String> getDataFromExternalApi() {
        return webClientBuilder.build()
            .get()
            .uri("https://api.example.com/data")
            .retrieve()
            .bodyToMono(String.class);
    }
}

30. How do you implement security in Spring Boot applications?

Spring Boot integrates with Spring Security to provide authentication and authorization capabilities.

Basic steps to implement security:

  1. Add the Spring Security dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  1. Create a security configuration class:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/public/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user").password(passwordEncoder().encode("password")).roles("USER")
            .and()
            .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

This configuration secures all endpoints except those under /api/public/** and uses in-memory authentication for demonstration purposes. For production applications, consider using database-backed authentication or OAuth2.

WeCP Team
Team @WeCP
WeCP is a leading talent assessment platform that helps companies streamline their recruitment and L&D process by evaluating candidates' skills through tailored assessments