Programming
AI/ML
Automation (RPA)
Software Design
JS Frameworks
.Net Stack
Java Stack
Django Stack
Database
DevOps
Testing
Cloud Computing
Mobile Development
SAP Modules
Salesforce
Networking
BIG Data
BI and Data Analytics
Web Technologies
All Interviews

Top 29 Spring Boot Interview Questions and Answers

11/Oct/2021 | 5 minutes to read

java

Here is a List of essential Spring Boot Interview Questions and Answers for Freshers and mid level of Experienced Professionals. All answers for these Spring Boot questions are explained in a simple and easiest way. These basic, advanced and latest Spring Boot questions will help you to clear your next Job interview.


Spring Boot Interview Questions and Answers

These interview questions are targeted for Spring Boot for Java Developers. You must know the answers of these frequently asked Spring Boot interview questions to clear the interview.


1. What is the Spring Boot?

Sprint Boot is an open-source, opinionated and easy to get-started project on the top Spring Framework which allows you to build stand-alone production-ready applications. You can develop the applications with minimal spring configuration and that you can just run. The main goals of Spring Boot include:
  • provides fast and easy to get-started experience for Spring application developments.
  • No code generation or no requirement of XML configuration.
  • To provide good features for all common projects like security, health checks, metrics, embedded servers and configuration.
  • We can go out of the way as requirements diverge from the defaults in spite of being opinionated.
For more visit Spring Boot.

2. What features does Spring Boot provide?

Spring Boot comes with a lot of features as below.

  • Used to create stand-alone Spring based applications.
  • No need to deploy 'WAR' files as Embedded Tomcat, Jetty or Undertow directly
  • Build configuration is simple as it provides opinionated 'starter' dependencies.
  • Provides capability to configure Spring and third party libraries automatically based on requirement.
  • Many production-ready features such as health checks, metrics, security and externalized configuration.
  • You can develop applications without additional requirements for XML configuration.

3. Differentiate Spring Boot vs Spring.

Let's try to understand the difference based on some features and how both frameworks can be used to develop the applications.
  • Spring Framework provides infrastructure and basic modules such as Spring JDBC, Spring MVC Spring Security, Spring ORM, Spring AOP and Spring Test to develop the Java applications. These modules help to reduce development time of an application.
    Spring Boot is on top of Spring framework that allows you to set up an spring application with minimal configuration or removes the boilerplate configurations. Spring Boot provides certain features as listed in the above question on the top of Spring Framework.
  • Spring requires many dependencies to make a web application up and running, on the other hand Spring Boot requires only one dependency spring-boot-starter-web to make application up and running. Other dependencies are automatically added during build time. Spring Boot provides many starter dependencies for different Spring Modules.
  • To enable MVC configuration you need a lot of configuration in Spring framework but Spring Boot requires only a couple of properties once you added web starter dependency as below.
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
  • To enable HTTP Basic Authentication, Spring requires both spring-security-web and spring-security-config, Spring Boot also requires these but spring-boot-starter-security dependency automatically takes care of these, you do not need to manually add those dependencies.
  • Bootstrapping - Servlet distinguishes bootstrapping of applications in Spring and Spring Boot. Web.xml or SpringServletContainerInitializer are used to bootstrap the Spring application. Whereas, Spring Boot uses Servlet 3+ approaches and a class which is annotated with @SpringBootApplication annotation to bootstrap the application .
For more differences between Spring and Spring Boot visit Spring vs Spring Boot.

4. What is Spring Initializr in Spring Boot?

Spring Initializr is a web based tool which provides an extensible API to generate easy-to-start JVM-based projects. You can also use it to inspect the metadata used to generate the project such as list the available dependencies and versions. You can visit 'Spring Initializr' web , fill you project details, select your options and download the bundled up project as a zip file. For more you can refer Spring Initializr.

5. What is the role of an Application Class?

Spring Boot applications has a configuration class named as Application which contains the main method to start or bootstrap the application. When you create an application using Spring Initializr, it automatically adds this class with required minimal configuration. Main method of this class uses the SpringApplication.run() method for bootstrapping the application as below code.

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

6. How to run a Spring Boot application?

Once you are ready with the required application configuration can run a spring boot application in different ways as below.
  • Running from an IDE - you can run your application from IDE as a simple Java application.
  • Running as a packaged application - if you created an executable jar using maven or gradle then you can use java -jar to run your application.
    $ java -jar target/myproject-0.0.1-SNAPSHOT.jar
  • Use Maven Plugin - you can use run goal of Spring boot Maven plugin to compile and run the application.
    $ mvn spring-boot:run
  • Use Gradle Plugin - you can use bootRun task of Spring Boot Gradle plugin to run your application in exploded form.
    $ gradle bootRun
For more you can visit Run your Spring Boot application.

7. Explain Spring Boot Architecture.

8. What is Auto-configuration functionality in Spring Boot?

Spring Boot provides the capability to auto configure the Spring applications based on Jar dependencies that you have added. You can use @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes to enable the auto-configuration.
If you want to disable any specific auto-configuration class you can disable that using the 'exclude' attribute as below.

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

    }
For more about auto-configuration visit Spring Boot Auto-Configuration.

9. What is @SpringBootApplication annotation in Spring Boot?

Most developers like to use some features such as auto-configuration, component scan and any extra configuration to be defined at application class in Spring Boot application. The @SpringBootApplication annotation allows you to enable below three features with single annotation.

  • @EnableAutoConfiguration - enable auto-configuration in Spring Boot application.
  • @ComponentScan - It enables the @Component scan on a package where an application is located.
  • @Configuration - allows you to import additional configuration classes or register extra beans in the context.
Example:

    package com.example.myapplication;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
    public class Application {

        public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    }
For more visit @SpringBootApplication annotation.

10. How to build an executable JAR in Spring Boot?

To create a JAR file you should have at least one class containing main method. You can create executable JAR file with Maven using various approaches as follows:
  • Using Manual Configuration - You need to use maven-dependency-plugin.
  • Using Apache Maven Assembly Plugin - It allows you to generate a single runnable package that contains all dependencies, modules and site documentation. It keeps only one goal which creates all other assemblies.
  • Using Apache Maven Shade Plugin - It allows you to package the artifacts in an uber-jar file that contains all required dependencies to run the application. It also supports shading - meaning renaming some of the dependency packages.
  • One Jar Maven Plugin - "One Jar project" allows you to load classes and resources from jars inside an archive instead of loading resources from jars inside a file system with the help of a custom class loader provided by One Jar Project.
  • Spring Boot Maven Plugin - This Plugin provides Spring Boot support in Maven Plugin which allows you to bundle the executable jar and war archives and allows the application to run "in place". You should have at least version 3.2 or later of Maven to use this approach.
To know more about each approach visit Create executable JAR file with Maven.

11. How to implement MicroServices using Spring Boot?

For more visit Microservices using Spring Boot.

12. How will you read HTTP Headers value in Spring boot REST Controller?

13. How will you implement exception handling in Spring Boot applications?

Spring Boot offers many ways to handle the exceptions and also provides a fallback error-handling page. Different exception handling ways are as follows.
  • Use of HTTP Status Codes - Custom exceptions can be annotated with @ResponseStatus annotation and throw that exception from the controller as below.
    
     @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Order")  // 404
     public class OrderNotFoundException extends RuntimeException {
         // ...
     }
        // use this method in controller as below
    
     @RequestMapping(value="/orders/{id}", method=GET)
     public String showOrder(@PathVariable("id") long id, Model model) {
         Order order = orderRepository.findOrderById(id);
    
         if (order == null) throw new OrderNotFoundException(id);
    
         model.addAttribute(order);
         return "orderDetail";
     }
                
  • Controller Based Exception Handling - You can handle the exceptions at controller level using @ExceptionHandler annotation.
  • Global Exception Handling - You can implement global exception handling using @ControllerAdvice annotation on any class as below.
    
    @ControllerAdvice
    class GlobalControllerExceptionHandler {
        @ResponseStatus(HttpStatus.CONFLICT)  // 409
        @ExceptionHandler(DataIntegrityViolationException.class)
        public void handleConflict() {
            // Nothing to do
        }
    }
                
  • If you want to go deeper you can implement HandlerExceptionResolver to set up your own custom exception handling.
For more you can visit Spring Boot Exception Handling.

14. What will happen if you use POST to get the data from the GET endpoint?

15. How to build RESTful APIs in Spring Boot?

16. How to implement Dependency Injection using Spring Boot?

17. What is Apache Kafka?

18. How to configure Apache Kafka works in Spring Boot application?

19. What is Spring Boot Actuator?

Spring Boot provides the capability for additional features which helps you to manage the application and code in a production environment. Spring Boot Actuator is a module that enables all production ready features in a Spring Boot Application. To add the actuator you need to add spring-boot-starter-actuator starter dependencies. If you are using Maven then you can add actuator as below:

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
          </dependencies>
    
If you are Gradle then you add an actuator as below.

    dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    }
For more visit Spring Boot Actuator.

20. How will you test a Spring Boot Application?

You can write unit tests for your application using a framework supported by Spring Boot. There are different ways to test Spring Boot application layers as below.

  • First, Add your Maven dependencies like spring-boot-starter-test that contains most of the elements required for unit tests.
  • JUnit - If you want to write tests using JUnit then you need to add JUnit Maven dependency - junit-vintage-engine.
  • You can perform Integration Testing with @SpringBootTest annotation and these should separate from unit tests as Integration Testing is time consuming. You need some more setup to perform Integration Testing. For this setup visit Integration Testing in Spring Boot.
  • When you don't want to use the actual server but want to test the layer below that, you can use @AutoConfigureMockMvc annotation that will allow your code to be called in the same way as it was called for the actual request.
  • If you want to test only the web layer then you can take the advantage of @WebMvcTest annotation.
For more visit Testing Spring Application.

21. Explain Service Registration and Discovery in Spring Boot.

22. Why is a Rest Controller required in Spring?

23. Explain different annotations in Spring Boot.

24. What is eureka service discovery?

25. What is feign client in Spring Boot? How does it work?

26. Differentiate @RequestParam vs @PathParam vs @PathVariable vs @QueryParam in Spring boot.

For more visit @RequestParam vs @PathParam vs @PathVariable vs @QueryParam.

For Sprint Boot Guide

Refer this - Spring Boot.

Some General Interview Questions for Spring Boot

1. How much will you rate yourself in Spring Boot?

When you attend an interview, Interviewer may ask you to rate yourself in a specific Technology like Spring Boot, So It's depend on your knowledge and work experience in Spring Boot.

2. What challenges did you face while working on Spring Boot?

This question may be specific to your technology and completely depends on your past work experience. So you need to just explain the challenges you faced related to Spring Boot in your Project.

3. What was your role in the last Project related to Spring Boot?

It's based on your role and responsibilities assigned to you and what functionality you implemented using Spring Boot in your project. This question is generally asked in every interview.

4. How much experience do you have in Spring Boot?

Here you can tell about your overall work experience on Spring Boot.

5. Have you done any Spring Boot Certification or Training?

It depends on the candidate whether you have done any Spring Boot training or certification. Certifications or training are not essential but good to have.

Conclusion

We have covered some frequently asked Spring Boot Interview Questions and Answers to help you for your Interview. All these Essential Spring Boot Interview Questions are targeted for mid level of experienced Professionals and freshers.
While attending any Spring Boot Interview if you face any difficulty to answer any question please write to us at info@qfles.com. Our IT Expert team will find the best answer and will update on the portal. In case we find any new Spring Boot questions, we will update the same here.