Spring Boot Interview questions

Spring Boot Interview questions | Freshers & Experienced

  • Subrata Dutta
  • 17th Nov, 2019
  • 1094 Followers

Q1. What is Spring Boot?

Spring boot is an open-source micro-framework based on Java. It is used to create stand-alone, production-ready Spring based Applications.

Q2. What is bootstrapping in Spring Boot?

Bootstrapping is a process of initializing an application. Bootstrapping in spring boot application is done using Spring Initializer. You can Bootstrap your spring boot application by visiting www.start.spring.io.

Q3. What is Thymeleaf in Spring Boot?

Thymeleaf is an template engine for XML/XHTML/HTML5 and Java library. It provides elegant, clean templates tag and can easily be integrated with Spring applications.

To use Thymeleaf in Spring Boot add the following configuration in your pom.xml file

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-thymeleaf</artifactId> <html xmlns:th="https://thymeleaf.org">  </dependency>  

Q4. What is auto configuration in Spring Boot?

In Spring Boot auto-configuration is used to configure an application based on dependencies that are available on the classpath. AutoConfiguration helps spring boot application in speeding up the development process.

Q5. What are Spring Boot Starters?

Spring Boot Starters are a set of dependency descriptors that are used to build Spring Boot-based Java applications rapidly and easily. Spring Boot provides many inbuilt Starters some of them are spring-boot-starter-thymeleaf, spring-boot-starter-artemis, spring-boot-starter-web, spring-boot-starter-activemq, spring-boot-starter-validation, etc.

Spring Boot Starter Example

Suppose we have to create a REST service in service in Spring boot. To create such an application we have to use libraries like Spring MVC, Tomcat and Jackson that have a lot of dependencies. Here Spring Boot starters help us instead of including these dependencies manually we can use spring-boot-starter-web. Using spring-boot-starter-web Starter in our application automatically include the required dependencies.What we have to do add the following code in our pom.xml file.

    org.springframework.boot
    spring-boot-starter-web

Q6. What is Spring Boot Actuator?

Spring Boot Actuator is a tool used to monitor, gather metrics, and understand the traffic of our application. It is a production-ready feature, and we can enable this feature by adding it to the pom.xml file.

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

Q7. How to include API Throttling in Spring Boot?

API Throttling is a process to control the usages of API consumption within a time. It limits the number of requests made for a resource in a unit of time.

Spring does not provide any inbuilt components for implementing API Throttling. To implement API Throttling we can use the RateLimitter class of Guava library.

For more, you can Read this Implementing Throttling in Java (Spring Boot) Article.

Q8. How to serve static files in Spring Boot?

Static files in spring boot are served from the resources directory. Spring boot automatically add static web resources located within following directories

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

You can change the default paths of static resources by adding the path in spring.resources.static-locations property of application.properties

Example

spring.resources.static-locations=/html/,/js/,/css/

Q9. How to implement pagination in Spring Boot?

In SpringBoot PagingAndSortingRepository extension of CrudRepository can be used to retrieve entities using the pagination and sorting abstraction. You can use the findAll(Pageable pageable) method to return the Pageable object.

Here is the complete Example of pagination & sorting in SpringBoot

Q10. How can I disable the Spring Boot default login page?

You can disable the Spring Boot default login page by setting security.basic.enabled=false in your application.yml or application.properties file.

Q11. What is the difference between a model object and a entity in Spring Boot?

A model object constitutes data in the pattern of MVC or model view controller. Whereas, an entity constitutes data in the pattern of ORM or object-relational mapping. To simply put, a model is where you can put your data and an entity includes a sole instance of the domain object extricated into the system as a report.

For model object, if you desire to display anything on your website, like the HTML page, presumably you possess a "user" model target and exhibit "user.firstname" in the HTML page of yours. Whereas an entity is saved in your database, and you may have the @Entity that moves to the "user" table in your database.

An entity in Spring Boot are nothing but a JOJO Representation of that that can be stored persistently in the database. An Entity represents a table in the database.

Q12. What is a handler method in Spring Boot?

The handler method in Spring Boot is a combination that has to be executed by objects. This acts as a mapping connecting handler objects and requests. As Spring MVC structure offers some ready-made execution, the developers can easily implement this interface to provide tailored mapping strategies.

This method helps you to identify suitable, proper controller bean via Dispatcher servlet. It recognizes controller bean through the URL and tallies the anonym in URL accompanied by the controller bean. When it is matched, it comes back to Dispatch servlet and eventually the servlet accomplishes the business process of the controller, also, returning ModelAndView object to the Dispatcher servlet.

Q13. How to convert an object to json in spring boot?

objectMapper.writeValueAsString() method is used to generate JSON from an object in Spring boot.

Example

ObjectMapper objectMapper = new ObjectMapper();
Fruit fruit = new Fruit("apple", "banana","guava");
String fruitAsJson = objectMapper.writeValueAsString(fruit);
 

Q14. What are 'spring-boot-starter' jars?

In Spring Boot, jars are present in the pattern of spring-boot-starter. The jars inside spring boot do not enclose any packages. You can imply the jars to the starter POMs using automatic tools like Gradle or Maven. You can also add the jars manually to the spring boot classes via a particular classpath.

For instance, if you want to use a spring boot starter jar attribute to access a database, then just include the jar dependency spring-boot-starter-data-jpa in your application to get the database.

Some artifacts of spring boot starter jars are:

  • org.springframework.boot:spring-boot
  • org.springframework.boot:spring-boot-starter-logging
  • org.springframework.boot:spring-boot-autoconfigure

Q15. How to change port for a Spring Boot application?

Spring boot server by default starts on port no. 8080. You can change the port for a Spring Boot application using the following ways

  • Using Property Files: This fastest and easiest way to change the port of Spring Boot. To change it simply assign desired port no to server.port property of your application.properties.
    server.port=8081
    
  • Programmatic Configuration: You can change the port programmatically by the setting server.port property when starting the application. Something like
    @SpringBootApplication
    public class CustomApplication {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(CustomApplication.class);
            app.setDefaultProperties(Collections
              .singletonMap("server.port", "8083"));
            app.run(args);
        }
    }
    
    Next, customize the server configuration by implementing the WebServerFactoryCustomizer interface:
    @Component
    public class ServerPortCustomizer 
      implements WebServerFactoryCustomizer {
      
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            factory.setPort(8086);
        }
    }
    
    
  • Using Command-Line Arguments: You can do it when you are packaging and running your application as a jar.

    Example

    java -jar spring-5.jar --server.port=8083
    

Q16. How to enable auto reload or hot reloading of Spring Boot application in Intellij IDEA?

You can use auto reload or hot reloading functionality on Intellij IDEA. By adding below dependencies.

<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-devtools</artifactid></dependency>

After adding the above dependencies follow the below steps.

  • Open the Settings --> Build-Execution-Deployment --> Compiler and enable the Make Project Automatically.
  • Click ctrl+shift+A and search for the registry and enable compiler.automake.allow.when.app.running
  • Restart your IDE.

Q17. What is the Spring Boot Initilizr?

Spring Boot Initilizr is a web tool, that can create or produce a Spring Boot planned formation for you. It provides intuitive, simple site UI, to generate and configure Spring-based implementation. This tool aids the developers to easily construct a prime project structure sans hassles or worries. It also assists in the construction of Gradle or Maven scripts which is required for the formation of an application. Also, Spring Boot Initilizr helps you regarding the issues related to dependencies, version, language, and packaging (jar or war), that are also an essential part of an application. Moreover, Spring Boot Initilizr offers flexibility, swiftness and even adds any configuration aiding you to concentrate on the other features rather on the infrastructure of your business.

Q18. What is the difference between RequestMapping and GetMapping in Spring Boot?

Difference between RequestMapping and GetMapping in Spring Boot

REQUEST MAPPING-It is an annotation useful for mapping HTTP GET requests onto a particular handler. It needs lengthy typed codes and applies to controller class and methods. The method attribute is also to be specified. This method can be used for determining both HTTP GET and HTTP POST requests.

GET MAPPING-It is an annotation specific for the GET request method, developed to overcome the shortcomings of request mapping. Here the method attribute is not given as Getmapping always maps to an HTTP GET method only.

Q19. How to enable HTTP/2 support in Spring Boot?

In spring boot version 2.1 and above you can enable HTTP/2 support by adding server.http2.enabled=true property to your .properties (or .yml) file.

Q20. What is the difference between Spring Boot and the Spring framework?

The difference between Spring Boot and the Spring framework

SPRING BOOT SPRING FRAMEWORK
It is useful to shorten the code length and thus helps us by providing a most uncomplicated method to develop a web application. It is a widely used JAVA EE Frameworks for developing various applications.
It has annotation configurations and default codes which takes less time involved in developing an application. It is useful for all layers of implementation in the development of an application.
Autoconfiguration, a unique feature automatically configures a class based on that requirement. It has a Dependency injection or IOC and handles transactions as special features.

About Author :

  • Author of Spring Boot Interview questions

    Subrata Dutta

    Subrata Dutta is IT Project Manager from Noida, Uttar Pradesh, India. He is an exceptional team player who is proactive, efficient, methodical and has strong interpersonal & people management skills.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.