Spring Boot Interview Questions - Well, spring boot is becoming one of the most required skills in the application development industry.
The string boot is continuously coming up with new ways to make the application building process more accessible.
Nowadays, companies want to hire excellent web developers in spring boot since it is the most used technology to create a web application with java.
This article will go through some of the most asked Spring Boot Interview Questions and the most effective way to answer those questions.
Before jumping to the interview questions, let us first understand what a spring boot is?
Most of you already know about Spring, but it is always better to revise, and sometimes it might be the first question an interviewer can ask in middle of your spring boot interview questions.
What is Spring Boot?
Spring boot is the open-source micro-framework that provides Java developers with a platform to get started with auto-configurable production-grade applications.
Using the spring boot, java developers can quickly develop an application without wasting time configuring their spring application. Spring boot is maintained by a company called Pivotal.
What are the Key Features of Spring Boot Which Make it Different?
● With spring boot, we can create stand-alone spring applications with minimum configuration needed.
● It has in-built servers, Tomcat, and jelly, which make it easy to run spring applications by coding.
● It comes with several built-in features such as metrics, health checks, and externalized configuration.
● It does not require XML configuration.
● Spring boot also has more application events and listeners, admin features, YAML support, Security, logging.
Spring Boot Interview Questions for Freshers:
1) What are the advantages of using Spring Boot?
The advantage of Spring Boot are listed below:
- Spring Boot makes the development of Spring-based applications fast and easy.
- No need to deploy war files.
- Spring Boot can create stand-alone applications.
- It is easy to embed Tomcat, Jetty, or Undertow into an application.
- No need for XML configuration.
- Spring Boot reduced the amount of source code.
- It has some additional functionality.
- It is very easy to start developing an application with Spring Boot since it is simple and easy to manage.
2) What are the minimum requirements for a Spring Boot System?
Spring Boot 2.1.7. Release requires
- Java 8 +
- Spring Framework 5.1.9. +
Direct build support:
- Maven 3.3 +
- Gradle 4.4+
Servlet Container support:
- Tomcat 9.0 - Servlet version 4.0
- Jetty 9.4 - Servlet Version 3.1
- Undertow 2.0 - Servlet Version 4.0
3) What are the key components of Spring boot?
The key components of Spring boot are as follows:
- Spring Boot auto-configuration.
- Spring Boot CLI.
- Spring Boot starter POMs.
- Spring Boot Actuators.
4) What are the features that Spring Boot offers which Spring doesn't?
Below are some of the key features which Spring Boot offers but Spring doesn't:
- Auto Configuration.
- Version Management.
- Starter POM.
- Component Scanning.
- Embedded Server.
- InMemory DB.
- Actuators.
It can also say that the Spring Boot makes the features of Spring easy to use for users.
5) What is the starter dependency of the Spring Boot module?
Below are some of the most used starter dependencies of Spring Boot:
- Test starter.
- Security starter.
- Web starter.
- Data JPA starter.
- Mail starter.
- Thymeleaf starter.
6) Explain the working of Spring boot. Or How does Springs Boot work?
The entry point of the Spring Boot application is the class that contains @SpringBootApplication Annotation and the main method.
The Spring Boot automatically scans all the components of the application by using @ComponentScan Annotation.
7) What does the @SpringBootApplication Annotation do?
@SpringBootApplication is the combination of three annotations which are @Configuration, @ComponentScan, and @EnableAutoConfiguration.
Spring Boot enables the developer to use a single annotation instead of using multiple.
8) What is the role of @ComponentScan in the class files?
The @ComponentScan Annotation enables component scanning so that controller classes and other Components you create will be automatically discovered and registered as beans in the Spring Application.
9) What does @Configuration Annotation do?
This Annotation marks a class as a configuration class for Java-based configuration. It is particularly important if you favor java-based configuration over XML configuration.
10) How does a Spring Boot application get Started?
The Spring Boot application is like any other Java program. It must have a main method that serves as the entry point and contains SpringApplication.run method to start the application.
Below is one example of Simple Spring Boot Application:
@SpringBootApplication
public class Myapp {
Public static void main(String[] args){
SpringApplication.run(Myapp.class);
// other statements
}
}
11) What does @RestController annotation do?
The @RestController Annotation helps you to add @ResponseBody and @Controller
Annotation to the Class.
12) What is the difference between @RestController and @Controller in Spring Boot?
@Contorller Map of the model object to view or template and make it human-readable, but @RestController returns the object, and object data is directly written in HTTP response as JSON or XML.
13) What are the Starter dependencies of Spring Boot?
Spring Boot starter comes with a maven template containing all the relevant transitive dependencies required to start a particular functionality.
It would be best if you imported spring-boot-starter-web dependency for creating a web application.
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
14) How to deploy a Spring boot application in Tomcat?
Tomcat server is already embedded in the Spring Boot. So whenever you run your application, Spring Boot will automatically detect the embedded Tomcat server.
In short, in Spring Boot, you don't have to manually deploy the application on the server.
15) What is the process to change the port in spring boot?
The default port to start the Spring Boot application is 8080. To change the port, you need to add the below property in your application. Properties file.
server.port=8081
You might also like:
- How To Become a Software Engineer after 10th: Top Computer Courses.
- Computer Science Engineering – Courses, Scope, and Eligibility.
- How To Become a Web Designer? – Top Web Designing Courses.
16) What is the Spring initializer?
Spring initializer Solves the problem of setting up a framework when you are starting a project from Scratch.
It is a web application that helps developers to create an initial Spring boot project structure and provides a maven or Gradle file to build your code.
17) What is a Spring Boot Actuator?
Spring Boot Actuator allows you to monitor and manage your application when you want to push it for production. It helps you to control your application by using HTTP endpoints.
18) How to enable/disable the Actuator?
In Spring Boot, it is very simple to enable/disable Actuator. To enable the Actuator, you must add the dependency to the spring-boot-starter-actuator, i.e., Starter, in your pom.xml file.
If you don't want the Actuator to be enabled, then don't add the dependency.
<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-actuator </artifactId>
</dependency>
</dependencies>
19) Name predefined endpoints that Actuators provide to monitor the Spring Boot Application?
Below are some of the predefined endpoints which Actuators provide to monitor the application:
- Health
- Info
- Beans
- Mapping
- Configprops
- Httptrace
- Heapdump
- Thread dump
- Shutdown
20) How to implement Security for Spring Boot application?
You have to use the spring-boot-starter-security starter dependency to your pom.xml file to enable the Spring security support in your Spring Boot application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<dependency>
21) What is the response entity in spring boot?
In Spring Boot, the response entity is HTTP response which includes the header, Status code, and body of your response.
22) How to handle exceptions in Spring Boot?
You can handle the exceptions globally by using @ControllerAdvice Annotation.
If you want to handle a specific exception and send a customized response, you need to use @ExceptionHandle Annotation.
23) What is Spring Boot DevTools, and how to use them?
Spring Boot Development tools(DevTools) is a set of tools making the development process easier.
To include these development tools, you just have to add a dependency to the pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-devtools</artifactId>
</dependency>
24) Which can use the UI web framework with Spring Boot?
The best UI web framework that It can use with spring boot is JHipster. By using JHipster, you can generate your web application and microservices within less time.
25) What is Spring Boot CLI?
Spring Boot CLI is a command-line interface that allows you to create a spring-based java application using Groovy.
26) What are the most common Spring Boot CLI commands?
The most commonly used CLI commands are as follows:
- -run
- -test
- -grap
- -war
- -install
- -uninstall
- --init
- -shell
- -help
To check the description of each command, just run Spring --help from the terminal.
27) How to create a war file in Spring Boot?
If it is a maven project, then to create a war file in spring boot, you need to define your packaging file as a war in your pom.xml; you can do it by changing the attribute value of packaging to war.
<packaging>war</packaging>
Then do maven clean and install so that your application will start building. Once the build is successful, go to the targeted folder to see the .war file generated for the application.
28) What are the endpoints in Spring boot?
Actuator endpoints allow you to monitor and interact with your spring application.
Spring Boot includes several built-in endpoints, and you can also add your own.
29) Which Annotation can be used to create a custom endpoint in Spring boot?
You can create a custom endpoint in SpringBoot by using @Endpoint Annotation.
30) What is the name of the configuration file which you can use in Spring Boot?
Application. Properties is the name of the configuration file used in Spring Boot. It is an important file that allows you to override your default configurations.
31) What dependency is used to integrate the Spring Boot application with ActiveMQ?
To integrate the Spring Boot application and ActiveMQ, spring-boot-starter-ActiveMQ dependency is used.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
32) What dependency is used to integrate the Spring Boot application with Apache Kafka?
To integrate the Spring Boot application and Apache Kafka, spring-Kafka dependency is used.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
33) How can we use Jetty instead of Tomcat in our web application?
Tomcat is a default embedded server in Spring Boot. So before switching to a different HTTP server, we have to exclude the default dependencies first and then add spring-boot-starter-jetty dependency in the pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<!-- Include Jetty instead-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
34) What is @pathVariable?
@pathVariable Annotation helps you to extract information directly from the URL.
Don't miss:
- Business Development Executive: Job, Role, Skills, Pros & Cons, Salary.
- Associate Product Manager: How to Become? Job, Role & Salary in 2021.
- B.Sc in Forensic Science: Scope, Eligibility, Skills, and Scholarships.
Advance Spring Boot Interview Questions:
1) Name the basic Annotation that Spring Boot offers:
Package org. Spring framework.boot.autoconfigure and its sub-package contain all the basic annotations that Spring Boot offers.
Below are some of the most used Spring Boot annotations:
- @Bean
- @Service
- @Repository
- @Configuration
- @Controller
- @RequestMapping
- @Autowired
- @Component
- @SpringBootApplication
- @EnableAutoConfiguration
- @ComponentScan
- @Required
- @Qualifier
- @CookieValue
- @Lazy
2) What is Spring Boot dependency management?
Spring Boot dependency Management is used to manage dependencies and configurations automatically without you specifying the version for any of those dependencies.
3) Can we use Spring Boot to create a non-web application?
Yes, we can use Spring Boot to create a non-web application. It can do it by removing the web dependencies from the classpath and changing the way Spring Boot creates the application context.
4) How to disable a specific auto-configuration class?
You can use the exclude attribute of @EnableAutoConfiguration if you want Auto-configuration, not to apply to any specific class.
//use of exclude
@EnableAutoConfiguration(exclude={className})
//className is the name of the class in which you want to disable auto
configuration
5) What is the difference between RequestMapping and GetMapping?
RequestMapping is the Annotation that It can use with getting, POST, RUN, and many other request methods by using the method attribute on the Annotation.
But GetMapping is an extension of RequestMapping, which helps us to improve the clarity of requests.
6) What is Pagination and how to do it in Spring Boot?
The process of dividing data into small and suitable sets is called Pagination. You can perform Pagination in Spring Boot by using PagingAndSortingRepository, which is an extension of crudRepository.
7) How to use a logger in Spring Boot?
In Spring Boot, there are many logging options available. Some of them are listed below:
- log4j2:-
To use log4j2 you have to import org.apache.logging.log4j2.Logger and org.apache.logging.log4j2.Logger packages.
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
Logger logger = LogManager.getLogger(LoggingController.class);
- Lombok:-
To use Lombok, you have to add an org.projectlombok dependency in your pom.xml file.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
Now you can create a logging controller and add the @Slf4j Annotation to it.
@RestController
@Slf4j
public class LoggingController {
@RequestMapping("/logging")
public String index() {
log.trace("A TRACE Message");
log.debug("A DEBUG Message");
log.info("An INFO Message");
log.warn("A WARN Message");
log.error("An ERROR Message");
return "Here are your logs!";
}
}
There are many more different ways to use logger in Spring Boot.
8) How to enable debugging logs in the Spring Boot?
There are three ways to enable debugging logs in Spring Boot-
- You can start the application with --debug switch.
- You can set the logging.level.root=debug property in the application. Property file.
- You can set the logging level of the root logger to debug in the supplied logging configuration file.
9) What is thyme leaf, and how to use it in Spring Boot?
Thymeleaf is a server-side Java template engine used in web applications.
Its main job is to bring a natural template for your web application and integrate it well with Spring Framework and HTML5 java web applications.
To use thyme leaf, you need to add spring-boot-starter-thyme leaf dependency in your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
10) What is Spring Data?
The main function of Spring Data is to make it easier for the developers to use relational and non-relational databases, cloud-based data services, and other data access technologies. In other words, Spring Data makes it easy to access data.
11) What is a YAML file?
YAML is a human-readable data serialization standard that can be used with all programming languages and is often used to write configuration files.
12) What are the advantages of the YAML file over the properties file, and what are the different ways to load the YAML file in Spring Boot?
The advantage of the YAML file over a properties file is that the data stored in the YAML is in a hierarchical format.
Due to these, it becomes very easy for the developers to debug if there is an issue.
The SpringApplication class supports the YAML file as an alternative to properties whenever you use the SnakeYAML library on your classpath.
The different ways to load a YAML file in Spring Boot:
- Use YamlMapFactoryBean to load YAML as a Map.
- Use YamlPropertiesFactoryBean to load YAML as properties.
13) What are the steps to connect the Spring Boot application to a MySQL database using JDBC?
Below are steps to connect your Spring Boot application to a MySQL database using JDBC:
Step 1: You should have a database in MySQL. If you don't have one, you can create a DATABASE named demo by running the below query in your MySQL server.
CREATE DATABASE demo;
Step 2: Now, create a table inside the demo database by running the below query.
CREATE TABLE employee(employee INT PRIMARY KEY NOT NULL
AUTO_INCREMENT, employee name VARCHAR(255));
Step 3: Add JDBC.mysql and web dependencies in your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
< /dependency>
Step 4: You have to configure the database; you can do it by adding database information to the application.properties file by using predefined keys.
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
Step 5: Now, in the controller class, you need to handle the requests.
package com.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JdbcController {
@Autowired
JdbcTemplate jdbc;
@RequestMapping("/save")
public String index(){
jdbc.execute("insert into employee
(name) values ('Azad Khan')");
}
}
Step 6: Your application is now connected to the MySQL database.
14) How to create a login page in Spring Boot?
To create a simple and default login page in Spring Boot by using Spring security.
Spring security secures all HTTP endpoints where the user has to login into the default HTTP form provided by Spring.
For this, you need to add spring-boot-starter-security dependency in your pom.xml or build.
Gradle and a default username and password can be generated with which you can log in.
15) What is dependency injection?
Dependency injection is the process of injecting dependent bean objects into target bean objects. There are three different ways to carry out dependency injection.
● Setter injection: The IOC container will inject the dependent bean object into the target bean object by calling the setter method.
● Constructor injection: The IOC container will inject the dependent bean object into the target bean object by calling the target bean constructor.
● Field Injection: The IOC container will inject the dependent bean object into the target bean object by Reflection API.
16) What is an IOC container?
IOC Container is a framework that implements automatic dependency injection. It also manages object creation and its lifetime and also injects dependencies into the class.
17) What is profile and its uses in Spring Boot?
Profile allows developers to segregate parts of their application configuration and make it available only in certain environments.
You can mark any @Component or @Configuration with @Profile to limit when it is loaded:
@Configuration
@Profile("production")
public class productionConfiguration {
//other statements
}
You can use a spring. Profile. Active Environment property to specify which profiles are active.
You can specify the property in any of the usual ways. For example, you could use it in your application. Properties.
spring.profiles.active=dev,hsqldb
or specify on the command line using the switch --spring.profiles.active=dev,hsqldb
18) How to ignore null values in JSON responses using Spring Boot?
You can use @JsonIgnore Annotation to ignore null values in the JSON response.
The null field can be ignored while reading JSON into Java objects and writing Java objects into JSON.
19) How to create the @GET endpoint in Spring Boot?
Below is the code to create the @GET endpoint in Spring Boot:
// Below are important packages to create @GET endpoint
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.practise.springboot.model.Product;
// Need to mention the RestController so that it will behave as a rest
endpoint.
@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo = new HashMap<>();
static {
Product Growwpedia = new Product();
honey.setId("1");
honey.setName("Growpedia");
productRepo.put(Growwpedia.getId(), Growwpedia);
Product Azad = new Product();
almond.setId("2");
almond.setName("Azad");
productRepo.put(Azad.getId(), Azad);
}
// Below method acts as a GET method, which is responsible for receiving the HTTP GET call and returning the Product details as a response along with the HTTP Status OK.
@RequestMapping(value = "/products", method = RequestMethod.GET)
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
}
}
Spring Boot Interview Questions: Wrap Up!
I had tried to include all the essential Spring Boot interview questions. I might have missed some crucial questions since including all the questions in one article is impossible.
If you have any doubt regarding spring boot interview questions, feel free to comment down. We will try to answer it as soon as possible.
nimabi says
Thank you very much for sharing, I learned a lot from your article. Very cool. Thanks.