Spring Boot Interview Questions - 2022 - Tech:443

Latest

Saturday 5 June 2021

Spring Boot Interview Questions - 2022

Spring Boot Interview Questions



Frequently asked spring boot interview questions.

  •     What is Spring? What is spring boot?

 

Spring:  Spring is a lightweight(basic version of Spring framework is around 2MB) Application Development framework for enterprise java. It provides comprehensive infrastructure support for developing Java Applications.

 Spring Boot: It is a spring module that offers Rapid Application Development to Spring frameworks with the extra support of auto-configuration and embedded application servers(like tomcat, jetty). It helps us in creating efficient fast stand-alone applications (as servers are embedded in spring boot) basically removes a lot of configurations and dependencies (boilerplate code).


  •    What is RAD?

RAD (Rapid Application Development) is modified Waterfall model which focuses on developing software in short span of time. It does not dedicate a lot of time or resources on planning and instead uses a method of prototyping to introduce the product.

 

RAD Phases :

·        Stage 1: Business Modelling

The business modelling step in the RAD model takes information from the company gathered through many business-related sources. This info is then combined into a useful description of how the data can be used when it is processed, and what is making this specific information successful for the industry.

 

·        Stage 2: Data Modeling

During the Data Modeling stage, all the information gathered during the Business Modeling phase is analyzed. Through the analysis, the information is grouped into different groups that can be useful to the company. The quality of each data group is carefully examined and given an accurate description. A relationship between these groups and their usefulness as defined in the Business Modeling step is also established during this phase of the RAD model.

·        Stage 3: Process Modeling

The Process Modeling phase is the step in the RAD model procedure where all the groups of information gathered during the Data Modeling steps are converted into the required usable information. During the Process Modeling stage, changes and optimizations can be done, and the sets of data can be further defined. Any descriptions for adding, removing, or changing the data objects are also created during this phase.

Stage 4: Application Generation

The Application Generation step is when all the information gathered is coded, and the system that is going to be used to create the prototype is built. The data models created are turned into actual prototypes that can be tested in the next step.

Stage 5: Testing and Turnover

The Testing and Turnover stage allows for reduced time in the overall testing of the prototypes created. Every model is tested separately to identify and adapt the components quickly to create the most effective product. Since most of the elements have already been examined previously, there should not be any major problems with your prototype.

 

  •       How to change the port of Embedded Tomcat server in Spring Boot?

Open application.properties in your project and add

server.port=7623

  •     Can we override or replace the Embedded Tomcat Server in Spring Boot?

Yes, We can replace it with any other server by using starter dependency in pom.xml.

 

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

<!—Adding Jetty Dependency->

<dependency>

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

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

</dependency>

 

  •    Can we disable the default web server in Spring Boot Application?

Yes, Add the following line in application.properties

spring.main.web-application-type=none

 

  •      How to disable a specific autoconfiguration class in spring boot?

Use exclude in the annotation

@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class})

 

  •       What does @SpringBootApplication do internally?

It is equivalent to using annotations i.e @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.

 

  •     How to use a property defined in application.properties file into a java class?

@Value(“$server.port”)

private int serverPort;

 

  •       What is @RestContoller annotation in spring?

@RestController is a convenience annotation for creating Restful Controllers. It is a specialization of @Component and is autodetected through classpath scanning. It adds the @Controller and @ResponseBody annotations. It converts the response to JSON or XML

Eliminate the need to annotate every request handling method of the controller class with the @ResponseBody annotation. It is typically used in combination with annotated handler methods based on the @RequestMapping annotation.

Data returned by each method will be written straight into the response body instead of rendering a template.

 

  •        How to change response to JSON in RestController?

@RequestMapping(value = "/{id}", produces = { MediaType.APPLICATION_XML_VALUE })

public getStudent(@PathVariable(value = "id") String studentId) throws IOException {

    return studentService.get(studentId);

}

 

  •        Difference between @Controller and @RestController in Spring Boot?

@Controller maps the model object to view or templates and makes it human readable whereas @RestController simply returns the object and object data is directly written into HTTP response as JSON or XML.

 

  •      What are the profiles in Spring Boot?

A profile is a set of configuration settings. Spring Boot allows defining profile-specific property files in the form of application-{profile}.properties. It automatically loads the properties in an application.properties file for all profiles, and the ones in profile-specific property files only for the specified profile. The keys in the profile-specific property override the ones in the master property file.

 

application-dev.properties 

application-test.properties 

 application-prod.properties

 

application.properties

spring.profiles.active=prod

 

  •  What is Spring Actuator?

It is an additional feature in Spring Boot that helps you monitor and manage your application when you push it to production. These features include Auditing, health and metrics gathering, and many more features that can be automatically applied to your application.

 

Add spring-boot-starter-actuator in pom.xml

 

Using Spring Actuator, you can access those flows like what bean is created, what is the CPU Usage, HTTP hits that your the server has handled.


  •  How to use Spring Actuator in Spring Boot Application?

Add dependency in pom.xml

<dependency>

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

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

</dependency>

 

Hit http://localhost:8080/actuator

 


  • What is @PathVariable annotation?

@PathVariable annotation helps you to extract information from the URI directly.



CHECK OUT: Spring Boot GitHub Repos for implemented spring boot concepts.