Over the past few months, I’ve received several requests for Spring Boot interview questions. In this article, I’ll cover 21 Spring Boot interview questions. In addition, I’ll cover some issues related to microservices architecture.

I’ve divided these Spring Boot interview questions into three categories — for novice, intermediate, and experienced developers.

1. What is Spring Boot? What is its use in Web application development?

Spring Boot is a framework built on top of the Spring Framework for rapid application development. Ruby on Rails, Django, and Python web frameworks are also well known. In particular, Spring brings Java a much needed Web framework for building Web applications. Important features such as embedded application servers and automatic configuration allow for rapid development and deployment.

  • The Spring Boot framework allows you to import dependencies easily.

  • It also simplifies the use of Spring Security in applications with its rich features.

  • Actuator- Enables monitoring of applications.

2. What is the difference between Spring Boot and Spring?

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

Similarly, the Spring framework provides data binding, dependency injection, aspect-oriented programming, data access, security, and more. In short, Spring Boot is one of the modules of the Spring framework. Spring Boot is built on top of the Spring framework. Spring Boot is a microservices-based framework for building applications. The auto-configuration feature in Spring Boot helps reduce lines of code.

3. How do I set up a Spring Boot application using Maven or Gradle?

To set up a Spring Boot application, you can use Maven or Gradle. Both are used to pull the required dependencies from the application. In particular, for spring Boot applications, you can pull the spring-boot-starter-parent dependency.

In Maven, we use the pom.xml file, and in Gradle, we use build.gradle for dependency declarations.

Maven:

< the groupId > org. Springframework. Boot < / groupId > < artifactId > spring - the boot < / artifactId > < version > against 2.4.1 < / version >Copy the code

Gradle:

Compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.4.1'Copy the code

4. How do I change the default port of a Spring Boot application?

There are three ways to change the default port of a Spring Boot application. The most straightforward approach is to use the application.properties or application.yml file.

  • Server.port uses 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 when starting the application as a JAR file.

5. How to use Spring Boot to set up an application with HTTPS port?

By default, Spring Boot applications run on HTTP ports. There are two ways to get your application to run using HTTPS ports. Can use for server port, server. SSL. Key – store – password for server SSL. Key – store, server SSL. The key – store – type and server SSL. Key – alias. Another way to use HTTPS ports is programmatically.

6. Which embedded servers does Spring Boot support?

Spring Boot applications support Tomcat, Jetty, and Undertow network servers. By default, Spring Boot’s Web initiator supports the Tomcat Web server.

To change the default Web server, you must exclude Spring-boot-starter-Tomcat and include the appropriate server launcher, such as 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>Copy the code

7. What are the advantages of Spring Boot?

Here are the main benefits of Spring Boot:

  • Allows you to create standalone Spring applications
  • Tomcat, Jetty, or Undertow servers can be easily used
  • The rapid development
  • It also allows easy use of microservices-based architectures

So far, I’ve covered some basic interview questions. If you are a developer with several years of experience in Java or Spring Boot development, the following set of questions will help you improve.

8. How do I deploy Spring Boot Web applications as JAR and WAR files?

Typically, we deploy our Web application as a WAR. We deployed the WAR file to an external Web server.

Obviously, if you build an application using Maven, Spring provides a plug-in spring-boot-Maven-plugin. You can then package the Web application as an executable JAR file. If you use Gradle, gradle Build creates an executable JAR file by default.

Maven builds a JAR file from jar. War will build a WAR file. If no packaging elements are included, Maven will generate jar files by default. You also need to keep container dependencies as follows

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

For Gradle, you need to mark the embedded container dependencies as belonging to the WAR plug-in providedRuntime configuration.

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

For applications that use automatic configuration, component scanning, and additional configuration, you can include an annotation @SpringBootApplication. This annotation includes three annotations @enableAutoConfiguration, @ComponentScan, and @Configuration.

  • @enableAutoConfiguration – Enables automatic configuration of Spring Boot
  • @ComponentScan – This allows you to scan the package the application is in.
  • Configuration – Allows registering additional beans or importing additional Configuration classes in context.

10. What are @restController and @RequestMapping annotations used for?

@restController — This annotation adds the @Controller and @responseBody annotations. This annotation marks a class that will process the incoming request. Typically, this annotation is used to create RESTful apis.

RequestMapping – This annotation provides routing information in the class that processes the incoming request. This annotation maps the incoming HTTP request to the corresponding method.

11. Why do we need Spring Porfiles?

When building enterprise applications, we use different environments, such as development, test, QA, and production. Spring Boot allows you to configure different application properties for each environment.

To help separate the configuration of each environment, you can name the properties files according to the environment, such as application-dev. Properties, application-test.properties, and application-prod.properties.

12. How do I disable a specific auto configuration?

To disable any specific autoconfiguration, use the exclude attribute of the @enableAutoConfiguration annotation.

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

13. What is Thymeleaf and how to use it?

Thymeleaf is a server-side template engine for Web applications. Its main purpose is to introduce templates into Web applications.

Specifically, to use Thymeleaf, you must include a dependency on Spring-boot-start-Thymeleaf in your application.

14. What are the different Spring Boot initiators and what are their uses?

It is emphasized that a major advantage of Spring Boot is the ability to use a variety of dependencies for Spring Boot applications.

All of the launcher Spring boot dependencies are under the org.Spring Framework. These dependencies are prefixed from spring-boot-starter-.

There are more than 50 initial dependencies, but here’s a list that most developers will use in their applications:

  • Spring-boot-starter – Indicates the core initiator. This includes automatic configuration, logging, and YAML.
  • Spring-boot-starter-data-jpa — This includes using Hibernate’s Spring Data JPA.
  • Spring-boot-starter-security — The Spring security model provides security for your Web applications. By default, it adds basic form-based authentication.
  • Spring-boot-start-web — This includes a Web module to help create RESTful web applications using Spring MVC.
  • Spring-boot-starter-thymeleaf — Importing this module allows developers to use the Thymeleaf template engine in their applications.
  • Spring-boot-starter-jdbc — This module helps connect to the application’s database.
  • Spring-boot-starter-tomcat — Usually, this is part of the Spring Boot parent module. This allows developers to use the embedded Tomcat servlet container.

Spring Boot interview questions for experienced developers

The next set of questions is for experienced developers. Despite these differences, beginner and intermediate developers alike should be aware of these issues.

15. How do I connect a Spring Boot application to a database using JPA?

Spring Boot provides spring-boot-starter-data-jPA dependencies to connect Spring applications to relational databases.

Spring Data JPA, on the other hand, handles the complexities of JDBC-based database access and object-relationship modeling. Therefore, it improves the implementation of data access layer processing. From a developer’s perspective, it reduces the dependence on relational database queries.

JPA allows application classes to be mapped to database tables.

16. Why use Spring Boot Actuator?

Spring Boot Actuators bring production-ready features to apps. As a result, the application can be easily monitored.

It also provides audits, health checks, and different metrics for applications. Therefore, you need to include the following dependencies:

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

Here are some key points that this dependency provides for your application:

Env – Displays all environment attributes

Health – Displays the health of the application

Metrics — Displays the various metrics for the application

Loggers – displays the configuration of logs in the application

Httptrace — Displays HTTP trace information

17. What is the purpose of Spring Boot Devtools?

Spring Boot DevTools or Developer Tools is a set of Tools that make the development process easier. To include DevTools support, you must add a spring-boot-DevTools dependency to your project.

Spring Boot DevTools helps disable caching used by many Spring Boot dependencies. This is useful during development, because developers want to see changes immediately.

In addition, using this library allows the application to be restarted if a file on the classpath changes. The library also supports debug logging for the Web.

18. What are the advantages of using Spring Data JPA?

Spring Data Jpa provides JpaTemplate to integrate Spring applications. You can also choose a specific implementation of the JPA specification.

By default, it is Hibernate.

Spring Data JPA benefits:

  • Reduce boilerplate code.
  • The generated queries help reduce the developer’s dependence on database queries.
  • The Repository pattern allows developers to easily handle persistence.

19. Spring Boot supports binding relaxation.

In general, the property’s key needs to exactly match the property name in the Spring Boot application. Spring Boot supports relaxed binding, which means that a property’s key doesn’t have to match its name exactly.

In addition, environment attributes can be written in any case. For example — if you have a property propertyDB in your bean class with the annotation @ConfigurationProperties, then you can bind that property to any of these environment properties — propertyDB, propertyDB, or property_DB.

20. How do I use Spring Security if I want to use it in my applications? How does it make the application secure?

Currently, to secure Spring Boot applications, you can include spring-boot-starter-Security dependencies in your project. By default, it will add some security measures to your application. These default measures include adding basic form-based authentication to the application or securing the REST API.

Then, in order to use Spring Security, can expand WebSecurityConfigurerAdapter class to add custom Security measures. In the same class, you will use the @enableWebSecurity annotation. This annotation allows Spring to look up the configuration and apply the class to global WebSecurity.

21. What do you do with comments @controllerAdvice?

The @controllerAdvice annotation helps manage exceptions in Spring Boot applications. In short, it is an interceptor that intercepts exceptions thrown by @requestMapping annotated methods.

The base class for ResponseEntityExceptionHandler is to provide centralized exception handling.

So far, I’ve covered Spring Boot interview questions.