30 Spring Boot Interview Questions [Updated 2021]

Spring Boot is a powerful Java framework from Pivotal. This article covers the most common Spring Boot interview questions for experienced and newbie candidates.

1. What is Spring Boot?

Ans. Spring Boot simplifies development and makes it faster. It provides a set of useful features that are built-in to the framework. These features include auto-configuration, RESTful web services, database, security, and much more.

Spring Boot also allows developers to create their own applications while still taking advantage of all the features provided by the framework.

2. What are some examples of features that come with Spring Boot?

Ans. Some examples are Actuator endpoints for exposing information about your application such as health checks or live metrics, Servlet support out of the box, creation of standalone spring applications.

3. What Spring problems do Spring Boot address?

Ans. Spring applications require developers to perform a lot of configuration. For example, developers need to know and specify all the dependencies, configure the data sources to connect to the database, do the front-end configuration like setting up the view resolvers (for a web application), etc. When Spring Boot is used, developers do not need to do any of this. Developers just need to configure the type of application being developed and Spring Boot automatically configures the application using suitable default values.

4. How can you install Spring Boot?

Spring Boot can be installed like any other Java library, you just need to include the appropriate JAR files on the classpath. Spring Boot provides Maven as well as Gradle dependencies. In addition, Spring Boot also supports a command-line interface (CLI). This is available as a compressed zip/tar.gz file and can be installed simply by uncompressing these files.

5. What is Spring Boot CLI? Explain how it can be used to get started with Spring Boot

Ans. Spring Boot CLI is a command-line interface that can be used to get started with Spring Boot easily. It lets you run Groovy scripts which help to get started with Spring Boot without requiring all the boilerplate code.  In order to use it, you need to do the following:

  1. Download and install the Spring Boot CLI
  2. Create a Groovy script
  3. Run the groovy script using spring run <groovy_script_name>

6. How does Spring Boot auto-configuration work?

Ans. Spring Boot auto-configuration simply means that Spring Boot configures the application based on the dependencies on the classpath. Autoconfiguration can be enabled by specifying either the @EnableAutoConfiguration annotation or the @SpringBootApplication annotation. This then configures the application automatically by scanning the classpath and registering beans based on the jar files available on the classpath. So, for example, if Spring Boot finds MySQL database driver on the classpath, it automatically configures a MySQL data source.

7. Explain how you can disable Spring Boot autoconfiguration.

Ans. In order to disable autoconfiguration, you need to specify the exclude attribute either on the @SpringBootApplication annotation or the @EnableAutoConfiguration annotation as shown below:

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

OR 

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 

public class MyApplication { 

}

This code disables DataSourceAutoConfiguration. So, a data source will not be automatically configured based on the dependencies on the classpath.

Autoconfiguration can also be disabled via the spring.autoconfigure.exclude property.

8. What are Spring Boot starters? List some important Spring Boot starters

Ans. Spring Boot starters are nothing but some dependency descriptors that can be included in the POM file. They help to get started with Spring Boot easily. When a starter is specified, the starter in turn adds all the necessary jar files required for the particular starter.

Some important Spring Boot starters are as follows:

  1. spring-boot-starter-data-jdbc – This is a JDBC starter. It can be used to get started with Spring Data JDBC
  2. spring-boot-starter-test – This is a starter test required to get started with testing
  3. spring-boot-starter-web – This is a web starter and can be used to create a web application/REST application

9. Explain the spring-boot-starter-data-jpa

Ans. The spring-boot-starter-data-jpa is a JPA starter and can be used to get started with Spring Data JPA with Hibernate in an application. It can be added using the following Maven dependency:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

This adds the dependencies for Spring Data JPA, Hibernate and Spring ORM.

10. Explain how you can develop a REST application using Spring Boot

Ans. In order to develop a REST application using Spring Boot, you need to do the following:

  1. Include the spring-boot-starter-web. This automatically includes an embedded Tomcat container.
  2. Create a Controller class that has the @RestController annotation and provide code for the endpoints that you’d like to handle
  3. Create a class that has the @SpringBootApplication annotation and the main method that starts the application

11. Explain the @SpringBootApplication annotation

Ans. The @SpringBootApplication annotation is specified on the main class of an application. It tells Spring Boot to scan for Spring components within the current package. It also tells Spring to enable auto-configuration (creating beans automatically based on the classpath). So, it is equivalent to specifying the @EnableAutoConfiguration, @Configuration, and the @ComponentScan annotations.

12. Explain how you can create a Spring Boot application using the Spring Initializer

 Ans. Spring Initializer is a web-based service that helps to get started with a Spring application easily. In order to create a Spring Boot application using Spring Initializer, you need to open up https://start.spring.io. Here, you need to select values for the type of project being created, type of dependencies required, type of build system being used (Maven or Gradle), etc. It then generates a project structure for you based on your build system. So, if you choose Maven, it generates a Maven project for you which can then be easily imported into your IDE.

13. Explain the Spring Boot main method

Ans. While creating a Spring Boot application, you need to create a class with the main method as follows:

@SpringBootApplication

public class SpringBootApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootApplication.class, args);

}

}

The class needs to be designated with the @SpringBootApplication annotation. Within the main method, you need to invoke the SpringApplication.run method as shown above. This allows running a Spring Boot application as a standard Java application.

14. How can you develop a web application that uses a Jetty embedded server using Spring Boot?

Ans. In order to develop a web application, Spring Boot provides a spring-boot-starter-web dependency. This includes the embedded Tomcat server by default. However, if you’d like to use Jetty instead of Tomcat, you need to specify the following in the Maven POM file:

<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>

This causes Tomcat to be excluded and Jetty to be included.

15. Explain the application.properties file used in Spring Boot

Ans. All the configuration information required by Spring Boot needs to be specified within the application.properties file. This can include properties related to database configuration, logging configuration, security-related properties, server related properties, etc. The following are some of the properties that can be specified within the application.properties file:

  1. config – Path of the logging configuration file
  2. datasource.url – Specifies the database URL
  3. port – Specifies the HTTP port

16. How can you configure SSL support in your Spring Boot application?

Ans. In order to configure SSL support in a Spring Boot application, the following needs to be done:

  • Obtain an SSL certificate (A self-signed certificate can be obtained via keytool)
  • Specify the following SSL related properties:
server.port=8443

server.ssl.key-store=<key store file path>

server.ssl.key-store-password=< key store password>

server.ssl.key-store-type=<key store type>

server.ssl.key-alias=<key store alias>
  • Restart the application. This causes Tomcat to start on port 8443.

17. How can you write a Spring Boot REST service that produces an XML output?

Ans. By default, a Spring BOOT REST service produces a JSON output. In order to produce an XML output, you can do one on the following:

Add the following jackson-dataformat-xml dependency as follows:

<dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

</dependency>

If you are using Java 8, you do not need to add any additional dependency. You just need to add the XmlRootElement annotation to your class

If you are using a Java version other than Java 8, you can also add the jaxb dependency as follows:

<dependency>

<groupId>org.glassfish.jaxb</groupId>

<artifactId>jaxb-runtime</artifactId>

</dependency>

However, this will also require you to annotate your class with the @XmlRootElement annotation.

18. List Spring Boot’s embedded containers.

Ans. Spring Boot supports Tomcat, Jetty, and Undertow embedded containers for servlet applications. Tomcat is the default container that is included when the spring-boot-starter-web dependency is used.  Spring Boot also supports the Reactor Netty, Jetty, Undertow, and Tomcat for webflux application. In this case Reactor Netty is the default and is included with the spring-boot-starter-webflux dependency.

19. How can you add ThymeLeaf support to your Spring Boot application?

Ans. In order to add ThymeLeaf support to a Spring Boot application, you need to do the following:

  • Add the following dependency to the POM file:
<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>
  • Create HTML files corresponding to the Thymeleaf templates and place them in the templates folder of your application
  • Redirect to the Thymeleaf templates from within your controller

20. How can you run a Spring Boot application?

Ans. You can run a Spring Boot application in one of the following ways:

  • Directly from an IDE – You can run a Spring Boot application directly from an IDE as a Java application
  • As a Jar – You can create an executable jar and then run your Spring Boot application using the following command:

java -jar <jar-file-path>

  •  Via the Spring Boot Maven plugin – The Spring Boot maven plugin can be used to run a Spring Boot application as follows:

mvn spring-boot:run

  •  Via the Spring Boot Gradle plugin – The Spring Boot Gradle plugin can be used to run a Spring Boot application as follows:

gradle bootRun

21. How can you specify properties via the command line while running a Spring Boot application?

Ans. Properties can be specified at the command line as a command-line option using – followed by the property name. For example, suppose you want to enable http2 via the command line, you can specify the server.http2.enabled property at the command line as follows:

java -jar <jar-file-path>  —server.http2.enabled=true

22. Explain how logging works with Spring Boot

Ans. By default, Spring Boot uses Logback as the logging library. Logback is automatically included when you add the Spring Boot starters (For example, the spring-boot-starter-web includes the spring-boot-starter-logging which includes support for Logback). Also, Spring Boot configures logback with default values so developers do not have to do any configuration. By default, the logging level is set to INFO and the logs are printed on the console. However, this can be changed easily by specifying the appropriate properties.

23. How can you configure Spring Boot with MongoDB?

Ans. In order to configure Spring Boot with MongoDB, you need to include spring-boot-starter-data-mongodb starter. You also need to write code as follows:

import org.springframework.data.mongodb.MongoDatabaseFactory;

import com.mongodb.client.MongoDatabase;

@Component

public class MongoDBBean {

private final MongoDatabaseFactory mongoDBFactory;

@Autowired

public MongoDBBean(MongoDatabaseFactory mongoDBFactory) {

this.mongoDBFactory = mongoDBFactory;

}

public void connectToDB() {

MongoDatabase database = mongoDBFactory.getMongoDatabase();

// other database code

}

}

By default, this code tries to connect to a MongoDB server at mongodb://localhost/test

24. Which libraries does the spring-boot-starter-test provide?

Ans. The spring-boot-starter-test is a Spring Boot starter that can be used to enable testing. It provides the following libraries:

  1. JUNIT – Java unit testing library
  2. Mockito – Mocking framework
  3. Hamcrest – Matcher library
  4. AssertJ – An assertion library
  5. JsonPath – XPath for JSON
  6. JSONAssert – JSON assertion library

25. How can you configure Spring Boot to use an in-memory database?

Ans. Spring Boot supports H2, Apache Derby and HSQL in-memory databases. You just need to specify the dependency of the database that you’d like to include and Spring Boot then auto-configures the database using suitable defaults. For example, if you’d like to use H2, you need to specify the following in the Maven POM file:

<dependency>

<groupId>com.h2database</groupId>

<artifactId>h2</artifactId>

<scope>runtime</scope>

</dependency>

This automatically configures an in-memory H2 database that connects to a database called testdb with username as sa and password as empty. If you’d like to customize the configuration, you can specify different values as specified below:

spring.datasource.url=jdbc:h2:mem:<db name>

spring.datasource.driverClassName=org.h2.Driver

spring.datasource.username=<username>

springspring.datasource.password=<password>

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

26. Which JSON libraries are supported by Spring Boot?

Ans. Spring Boot supports Jackson, JSON-B, and Gson JSON libraries. Spring Boot automatically configures the JSON library based on the dependencies on the classpath. So, for example, if Gson is present on the classpath, it automatically configures Gson. Jackson is the default JSON library. It is part of the spring-boot-starter-json.

27. Explain with a code sample how you can use JAX-RS for handling REST services using Spring Boot?

Ans. Spring Boot supports JAX-RS with Jersey. In order to use this, you need to add the spring-boot-starter-jersey starter.  You need to create a class that registers all the endpoints as follows:

@Component

public class MyConfig extends ResourceConfig {

public MyConfig () {

register(MyService.class);

}

}

Finally, you need to create a class corresponding to the endpoint as follows:

@Component

@Path("/sayhello")

public class MyService {

@GET

public String sayHello() {

return "Hello";

}

}

28. What is the use of Spring Boot Dev Tools?

Ans. Spring Boot Dev Tools includes some tools that make the development process easier. It can be enabled by adding the spring-boot-devtools starter. Once added, it supports features like automatic application restart (when there is a change in the classpath), live reload (automatically triggering a browser refresh when there is a resource change), Disabling cache, Enabling debug level logging, etc. All these features are very useful in a development environment.

29. What is the use of the @SpringBootTest annotation?

Ans. The @SpringBootTest application can be used for integration testing. It creates and initializes an ApplicationContext (which is required for a Spring application) and automatically starts the embedded container. It needs to be specified in a test class. Dependencies can also be injected in the test classes via the @Autowired annotation.

30. How can you display a custom error page when an error occurs in a Spring Boot based web application?

Ans. Spring Boot provides a class called BasicErrorController. In order to display a custom error page, you simply need to create an HTML file corresponding to the error page and place it in the resources/templates directory of your application. This then gets picked up BasicErrorController.  If you’d like to further customize the error handling then you need to create a class that implements the ErrorController interface.

31. How can you create an executable JAR of a Spring Boot application?

Ans. In order to create an executable JAR of a Spring Boot application, the following needs to be added to the Maven POM file:

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

You can then create an executable jar file using the following maven command:

mvn package

This creates an executable jar file in the target directory of your computer.

32. Explain how a Spring Boot application can be secured

Ans. In order to secure a Spring Boot application, the spring-boot-starter-security needs to be included as follows:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

You also need to create a class that extends WebSecurityConfigurerAdapter and provide appropriate implementation for the configure method.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Subscribe to get IQ's , Tutorials & Courses