Spring Boot Tricky Interview Questions & Answers
This guide compiles challenging and scenario-based Spring Boot interview questions. It covers topics ranging from internal auto-configuration mechanisms to deployment strategies, error handling, and reactive programming.
1. Core Spring Boot Mechanics & Auto-Configuration
Q: How do you tell an auto-configuration to back away when a bean already exists in Spring Boot?
A: You use the @ConditionalOnMissingBean annotation. This tells Spring Boot to create a bean only if it doesn't already exist in the application context.
For example, if you are writing a custom auto-configuration for a DataSource but want to back off if the developer manually defines one, you annotate your auto-configuration method with @ConditionalOnMissingBean(DataSource.class). This ensures the developer's custom configuration takes precedence and Spring Boot doesn't override it.
Q: What does it mean that Spring Boot supports "Relaxed Binding"?
A: Relaxed binding means Spring Boot is highly flexible in how properties are defined in configuration files (application.properties or application.yml) and how they map to @ConfigurationProperties beans.
For example, if you have a property for a server port, Spring Boot understands server.port, server-port, server_port, and SERVER_PORT as the exact same property. This flexibility makes it easier to adapt to different environment variables or developer naming preferences without breaking the application.
Q: What are the basic annotations that Spring Boot offers?
A: * @SpringBootApplication: The core annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@RestController&@RequestMapping: Essential for creating RESTful web services, defining controllers, and mapping URL paths.@Service&@Repository: Marker annotations for the business and data access layers, promoting a clear separation of concerns.@Autowired: Enables automatic dependency injection to wire beans together.
2. Deployment & CI/CD
Q: How do you deploy a Spring Boot web application as a JAR and WAR file?
A: * JAR File: By default, Spring Boot packages applications as executable JARs containing an embedded server (like Tomcat). You simply run mvn package and execute the application using java -jar application.jar.
- WAR File: If you need to deploy to an external standalone server (like a traditional Tomcat or Jetty installation), you must:
- Change the
<packaging>tag in yourpom.xmlfromjartowar. - Make your main application class extend
SpringBootServletInitializer. - Run
mvn packageto generate the.warfile, which can then be placed in thewebappsfolder of the external server.
- Change the
Q: Can we override or replace the embedded Tomcat server in Spring Boot?
A: Yes. If you prefer a different embedded server like Jetty or Undertow, you simply exclude the Tomcat dependency from the spring-boot-starter-web in your pom.xml (or build.gradle) and include the dependency for the server you want. Spring Boot will automatically detect the new server library on the classpath and auto-configure it as the embedded server.
Q: Discuss the integration of Spring Boot applications with CI/CD pipelines.
A: CI/CD integration automates the building, testing, and deployment of a Spring Boot application. Using tools like Jenkins, GitLab CI, or GitHub Actions, the pipeline is triggered whenever code is pushed. It automatically compiles the Java code, runs unit and integration tests, and packages the application (using Maven/Gradle). If all checks pass, the artifact (JAR/Docker image) is automatically deployed to a staging or production environment, drastically speeding up updates and catching errors early.
3. Error Handling & Pagination
Q: How do you resolve the "Whitelabel Error Page" in a Spring Boot application?
A: The "Whitelabel Error Page" appears when an unhandled exception occurs or a requested URL doesn't map to any controller. To fix or replace it:
- Ensure your
@RequestMappingor@GetMappingannotations correctly match the URLs you are trying to access. - Provide a custom HTML error page by simply placing an
error.htmlfile in yoursrc/main/resources/public/error/directory. - Use
@ControllerAdviceand@ExceptionHandlerannotations to handle exceptions globally and return customized, user-friendly JSON responses or views.
Q: How to handle a 404 Not Found error specifically in Spring Boot?
A: You can create a custom error controller by implementing the ErrorController interface and marking it with @Controller. Inside, you map a method to the /error path. In this method, you can inspect the HTTP status code. If it is 404, you can return a specific, helpful custom 404 HTML page or a structured JSON error message instead of the default Whitelabel page.
Q: How can you implement Pagination in a Spring Boot application?
A: Pagination is implemented using Spring Data JPA's Pageable interface.
- In your repository, ensure your query methods accept a
Pageableobject as a parameter. - In your service or controller layer, create an instance of
PageRequest.of(pageNumber, pageSize). - Pass this
PageRequestto the repository method. Spring Data JPA automatically intercepts this, handles the SQLLIMITandOFFSETlogic, and returns aPage<T>object containing the subset of data along with metadata liketotalPagesandtotalElements.
4. Architecture & Advanced Features
Q: How can Spring Boot be used to implement Event-Driven Architecture?
A: Spring Boot supports event-driven architectures natively through its Application Events.
- You create a custom event class extending
ApplicationEvent. - You use the
ApplicationEventPublisher(which can be autowired) to publish the event (publisher.publishEvent(new MyEvent())). - You set up listener methods annotated with
@EventListener(or@TransactionalEventListener) to react to these events. This completely decouples the publisher from the listeners, allowing different parts of the application (like sending an email after a user registers) to communicate without being tightly connected.
Q: Discuss the integration and use of Distributed Tracing in Spring Boot applications.
A: In a microservices architecture, a single user request might travel through multiple independent services. Tools like Spring Cloud Sleuth (or Micrometer Tracing in newer versions) and Zipkin provide distributed tracing.
They assign and propagate a unique Trace ID across all services involved in a request, along with Span IDs for each individual service hop. This allows developers to visualize the entire journey of a request, easily pinpointing which specific microservice is causing a delay or throwing an error.
Q: Your application needs to store and retrieve files from a cloud storage service. How would you integrate this?
A: I would use the cloud provider's official SDK (e.g., AWS SDK for S3 or Google Cloud Storage libraries).
- Add the SDK dependency to the
pom.xml. - Configure the credentials and bucket names in the
application.properties. - Create a dedicated
@Serviceclass to encapsulate the upload, download, and delete operations. By autowiring this service into the controllers, the cloud storage interaction remains clean, abstracted, and easily testable.
Q: How would you implement a "Soft Delete" feature in your Spring Boot application?
A: For a soft delete (where records are hidden but not physically erased for audit purposes), I would add a deleted boolean column or a deleted_at timestamp column to my database entity.
Instead of calling .delete() on the repository, I would simply update this column to true. To ensure these records don't show up in normal application queries, I would use Hibernate's @SQLDelete annotation to override the default delete behavior, and the @Where(clause = "deleted=false") annotation on the entity to globally filter out deleted records from all standard SELECT queries.
Q: Describe how you would implement Rate Limiting on your API endpoints.
A: To protect the application from abuse, I would use a library like Bucket4j or utilize the built-in rate limiting features of Spring Cloud Gateway (which uses Redis). By configuring these tools, I can define strict policies (e.g., "100 requests per minute per IP address"). If a user exceeds this limit, the API automatically intercepts the request and returns an HTTP 429 Too Many Requests status, ensuring fair usage and system stability.
Q: You are tasked with building a non-blocking, reactive REST API to handle a high volume of concurrent requests. Describe how you would use Spring WebFlux.
A: I would build the API using Spring WebFlux (by adding spring-boot-starter-webflux).
- In the controllers, instead of returning standard objects, I would return reactive types:
Mono<T>(for 0 or 1 item) orFlux<T>(for 0 to N items). - To ensure the entire stack is non-blocking, I would use reactive database drivers (like R2DBC instead of standard JDBC) and Reactive Spring Data repositories. This allows the application to handle massive amounts of concurrent traffic with a very small number of threads, heavily optimizing CPU and memory usage.