Top 21 Spring Boot Interview Questions

In the last few months, I have received a few requests about Spring Boot Interview Questions. In this post, I will cover the top 21 Spring Boot Interview Questions. Additionally, I will also cover some Microservice architecture related questions.

I have divided these Spring Boot interview questions into three categories – for a novice, for intermediate, and for an experienced developer. So if you don’t have any experience with Spring Boot, novice questions will help you.

Spring Boot Interview Questions for Novice Developers

1. What is Spring Boot and How is it useful in Web Application Development?

Spring Boot is a framework built on top of Spring Framework for rapid application development. Similarly, Ruby on Rails, Django with Python web frameworks are famous. Particularly, Spring brought a much-needed web framework to Java to build web applications. Important features like embedded application server and auto-configuration allow rapid development and deployment.

  • Spring Boot framework allows importing dependencies easily.
  • It also eases in using Spring Security in the applications with its rich features.
  • Actuator – One can implement production-ready features for monitoring the application.

2. What are the differences between Spring Boot and Spring?

Spring framework includes multiple features that can be used in the Spring Boot framework. On the other hand, the Spring Boot framework is used for application development.

Equally, Spring framework provides features like data binding, dependency injection, aspect-oriented programming, data access, security.

In short, Spring Boot is one of the modules of Spring Framework. Spring Boot is built on the top of the Spring framework. Spring Boot is a microservice-based framework to build applications. The feature of auto-configuration in Spring Boot helps in reducing lines of code.

3. How can you set up the Spring Boot application with maven or gradle?

To set up a spring boot application, one can use maven or gradle. Both are used to pull required dependencies in the application. Particularly, for spring boot application, one can pull spring-boot-starter-parent dependency.

In maven, one uses pom.xml file and in gradle, one uses build.gradle for dependencies declaration

Maven:

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>2.4.1</version>

Gradle:

compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.4.1'

4. How can you change the default port of Spring Boot Application?

There are three ways you can change the default port of the Spring Boot Application. Consequently, the most straight forward way is to use application.properties or application.yml file.

  • server.port use this property in application.properties.
  • You can also change the port programmatically in the main @SpringBootApplication class.
  • You can use the port of your choice while starting the application as a jar file. java -jar -Dserver.port=8980 myapplication.jar

5. How would you set up an application with HTTPS port with Spring Boot?

By default, the Spring Boot application runs on HTTP port. To make your application run with HTTPS port, there are two ways.

  • One can use server.port, server.ssl.key-store-password,server.ssl.key-store ,server.ssl.key-store-type and server.ssl.key-alias.
  • Another way to use the HTTPS port is programmatically.

6. What embedded servers does Spring Boot support?

Spring Boot application supports Tomcat, Jetty, and Undertow webservers. By default, the Tomcat web server is supported by Spring Boot’s web starter.

To change the default web server, one has to exclude spring-boot-starter-tomcat and include corresponding server starter like jetty or undertow.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

7. What are the advantages of Spring Boot?

Here are the main advantages of Spring Boot

  • Allows to create stand-alone Spring applications
  • One can easily use tomcat, jetty, or undertow server
  • Rapid Development
  • Also allows using Microservice-based architecture easily

Spring Boot Interview Questions for Intermediate Developers

So far, I have covered some basic interview questions. If you are a developer with a few years of experience in Java or Spring Boot Development, the next set of questions will help you where to improve on.

8. How to deploy Spring Boot Web Application as a jar and war files?

Generally, we deploy web application as web archive file (WAR). We deploy the war file on the external web server.

Evidently, Spring offers a plugin spring-boot-maven-plugin if using maven to build the application. You can then pack the web application as an executable jar file. If using gradle, gradle build will create an executable jar file by default.

For maven,

<packaging>jar</packaging> will build a jar file.

<packaging>war</packaging> will build a war file. If the packaging element is not included, the maven will jar file by default. You also need to keep the container dependency as follows:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

For gradle, you need to mark the embedded container’s dependencies as belonging to the war plugin’s providedRuntime configuration.

9. Why do we use the annotation @SpringBootApplication in the main Spring Boot application class?

For applications to use auto-configuration, component scans, and extra configurations, one can include one single annotation @SpringBootApplication. This annotation includes three annotations @EnableAutoConfiguration, @ComponentScan, and @Configuration.

  • @EnableAutoConfiguration – enable Spring Boot’s auto-configuration
  • @ComponentScan – this allows scanning the packages where the application is located.
  • @Configuration – allow registering extra beans in the context or import additional configuration classes.

10. What are @RestController and @RequestMapping annotation used for?

@RestController – This annotation adds annotations @Controller and @ResponseBody. This annotation marks a class that will handle the incoming requests. Usually, this annotation is used to create RESTful APIs.

@RequestMapping – This annotation provides routing information in the class that handles incoming requests. This annotation maps the incoming HTTP requests to the corresponding method.

11. Why do we need Spring Profiles?

When building enterprise applications, we use different environments like Dev, Test, QA, and Production. Spring Boot allows configuring application properties differently for each of these environments.

To help separate the configuration for each environment, one can name the properties file according to the environment like application-dev.properties, application-test.properties, and application-prod.properties.

12. How to disable specific auto-configuration?

To disable any specific auto-configuration, one can use exclude attribute for @EnableAutoConfiguration annotation.

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class SampleApplication
{
}

13. What is Thymeleaf and how to use it?

Thymeleaf is a server-side template engine for a web application. Its main purpose is to bring natural templates to the web application.

Specifically, one has to include the dependency spring-boot-starter-thymeleaf in the application to use thymeleaf.

14. What are the different Spring Boot starters out there and what purpose you can use them?

Emphatically, one major advantage of Spring Boot is to be able to use various dependencies for your Spring Boot application. All starter Spring Boot dependencies are under the org.springframework.boot group. These dependencies start with spring-boot-starter-.

There are more than 50 starter dependencies, but here is a list of dependencies that most developers will use in their applications:

  • spring-boot-starter – Core starter. This includes auto-configuration, logging, and YAML.
  • spring-boot-starter-data-jpa – This includes Spring data JPA with hibernate.
  • spring-boot-starter-security – Spring Security model offers security for your web application. By default, it will add basic form-based authentication.
  • spring-boot-starter-web – This includes a web module and helps to create web, RESTful applications using Spring MVC.
  • spring-boot-starter-thymeleaf – Importing this module allows the developer to use thymeleaf template engine in the application.
  • spring-boot-starter-jdbc – This module helps in connecting to the database for your application.
  • spring-boot-starter-tomcat – Usually, this is part of the spring boot parent module. This allows a developer to use an embedded tomcat servlet container.

Spring Boot Interview Questions for Experienced Developers

The next set of questions are for an experienced developer. Despite the distinction, a beginner as well as an intermediate developer should learn about these questions.

15. How do you connect Spring Boot Application to the database using JPA?

Spring Boot provides spring-boot-starter-data-jpa dependency to connect Spring application to a relational database.

On the other hand, Spring Data JPA handles the complexity of JDBC-based database access and object-relational modeling. Consequently, it improves the implementation of data access layer handling. From the developer’s point of view, it reduces the dependency on relational database querying.

JPA allows mapping application classes to database tables.

16. Why do you use the Spring Boot Actuator?

Spring Boot Actuator brings production-ready features to the application. As a result, one can easily monitor the application.

It also provides auditing, health check, and different metrics for the application. Therefore, you include dependency as follows:

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

Here are some of the endpoints that this dependency provides for the application:

  • env – Endpoint to display all the environment properties
  • health – Endpoint to show application’s health
  • metrics – This Endpoint displays various metrics of the application
  • loggers – Display the configuration of loggers in the application
  • httptrace – Endpoint to show HTTP trace information

17. What is Spring Boot Devtools used for?

Spring Boot DevTools or Developer Tools are a set of tools that make the development process easier. To include DevTools support, one has to add the dependency spring-boot-devtools in the project.

Spring Boot DevTools helps in disabling the caching that many spring boot dependencies use. This is useful in the development process as the developer wants to see the changes immediately.

Additionally, using this library allows applications to restart whenever there is a change in files on the classpath.

Moreover, the library enables debug logging for the web group.

18. What is the advantage of using Spring Data JPA?

Spring Data Jpa provides JpaTemplate to integrate Spring application with JPA. You can also choose a specific implementation of JPA specification. By default, it will be Hibernate.

Advantages of Spring Data JPA

  • Reduces boilerplate code
  • Generated Queries help in reducing developer’s dependency on database queries
  • Repository pattern allows developers to handle persistence easily.

19. Explain Spring Boot supports relaxed binding.

Generally, the key of a property needs to be an exact match of a property name in the Spring Boot application. Spring Boot supports relaxed binding which means the key of a property doesn’t have to be an exact match of a property name.

Moreover, the environment property can be written in any case. Example – If you have a property propertyDB in your bean class with annotation @ConfigurationProperties, you can bind that property to any of these environment properties – PROPERTYDB, propertydb, or property_db.

20. If you want to use Spring Security in your application, how do you use it, and how it makes the application secure?

Presently, to secure your Spring Boot application, you can include spring-boot-starter-security dependency in your project. By default, it will add some of the security measures for your application. These default measures include adding basic form-based authentication to your application or securing REST APIs.

Subsequently, to leverage the Spring Security, one can extend WebSecurityConfigurerAdapter class to add custom security measures. In the same class, you will use @EnableWebSecurity annotation. This annotation allows Spring to find configurations and apply the class to global WebSecurity.

21. What do you use the annotation @ControllerAdvice for?

@ControllerAdvice annotation helps in managing the exceptions in Spring Boot Application. In short, it is an interceptor of exceptions thrown by methods annotated with @RequestMapping.

ResponseEntityExceptionHandler is the base class that provides centralized exception handling.

So far, I have covered Spring Boot interview questions. I will still cover a few Microservices interview questions that can help developers.

Microservices Interview Questions

1. What is Microservices?

Microservices is an architectural style. It structures an application as a collection of services that are

  • Highly maintainable
  • Loosely coupled
  • Easily deployable
  • Owned by a small team

2. What are the common tools used to build and deploy microservices?

This depends on the technology stack. Docker remains one common tool irrespective of the technology stack. Hence, docker allows creating a container for the microservice. This container then can be deployed in the cloud environment.

Wiremock is another tool for flexible API mocking for microservices.

Hysterix is a circuit breaker that helps in latency and fault-tolerance of the application.

3. How does Spring help in Microservices development?

Most importantly, Spring helps in rapid development. Spring Boot helps in building REST APIs, web applications. Spring Cloud helps in integrating with external systems.

4. What are the challenges with Microservices?

Microservices rely on each other. Consequently, communication between these services needs to be secured. Since microservices are a distributed system, it can become a complex model. There can be an operational overhead. More services mean more resources.

Conclusion

In this post, I covered the most questions on Spring Boot that you might face during an interview. If you enjoyed this post, please subscribe to my blog here.