This interview question bank of with answer is suitable for candidates appearing for interview for the role of Spring Boot Senior developer or lead. It contains Spring Boot interview questions for 5 years experienced candidates. However those having 4 years to 7 years of experience developing in Spring Boot can leverage these questions and answers for upcoming interview.
Table of Contents
Difference between @Controller and @RestController
@Controller is a stereotype annotation to create a map of model object and to find a view. It cannot write directly to Http. For writing to Http, we need to annotate the method with @ResponseBody
Whereas @RestController is a specialized annotation which is combines both @Controller and @ResponseBody. Here response is directly written to Http Response as Json or XML. @ResponseBody is helping to serialize the response into Json using Jackson databind which is a transitive dependency of spring-boot-starter-web.
Difference between @CrudRepository and @JpaRepository
@CrudRepository – provides basic support for crud operations for find, save, delete etc
@JpaRepository extends PagingAndSortingRepository which extends CrudRepository. So, it inherits all the operations of crudrepository as well as pagination related functionalities.
Which Spring-boot-starter provides you embedded server?
Spring-boot-starter-web provides spring-boot-starter-tomcat as transitive dependency. This spring-boot-starter-tomcat provides embedded tomcat server to a spring boot application.
How to use any logging frameworks other than the default provided by spring boot?
The default logging framework provided by spring boot auto configuration is logback. To change it, we need to exclude the logback dependency from pom.xml which is a transitive dependency of spring-boot-starter-web. Then we need to add dependency for new logging framework in pom.xml and add configuration file for the changed logging framework under src/main/resources for updating the default configuration.
What are the ways we can provide spring configuration?
- XML based configuration
- Annotation based configuration
- Java based configuration
How to send logs to a file?
We need to add configuration in application.properties or application.yml with key as logging.file and value as the path and filename. Thus we can redirect logs to a file
Difference between logging.file and logging.path key in application.properties?
By using logging.file in application properties file, we tell spring boot the path and file name where the logs can be stored. However on using logging.path, we are just telling the path of file and not the log file name. Spring will create a file with name spring.log. If both are present in properties file then logging.file gets priority.
How to update the spring boot banner?
Banner is a spring logo shown in log. The default value is Spring which we can see in the log while starting server. It can be overwritten by creating a banner.text or banner.(gif/png/jpg) and keeping in the src/main/resources path. Whatever is mentioned here n the file, iwill be displayed will be written at the beginning of log when server starts next time.
How do you handle exception in spring boot?
In spring boot, we can create try catch block to handle exception and throw custom exception. Also we can use centralized exception handling to have the exception logged in specific predefined format. There are 3 ways by which we can handle exception.
- Use if else or try catch to handle the required scenario and return ResponseEntity from controller class with proper message and HttpStatus code
- Throw custom exception and annotate custom Exception Class with @ResponseStatus(code=<HttpStatus code>, reason=”<message>”)
- Create a centralized exception handler which is annotated with @ControllerAdvice and each method is related to each exception and are annotated with @ExceptionHandler(Exceptionname.class) – this gives more option to customize the message based on a specified standard across the application. We can create an exception model class which is used for returning exception details for all exceptions in the specific json format.
What is actuator in Spring boot?
Actuator provides production ready features for monitoring of application in spring boot. By default it exposes two endpoints /health and /info.
Can I expose all actuator end points?
To use actuator in a spring boot application, first you need to add actuator dependency in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
By default, only 2 endpoints are exposed – /health and /info. To expose all the endpoints, add management.endopints.web.exposure.include=*
How can I change base path of actuator endpoints?
The default base path of actuator is /actuator
So, the endpoint for /health endpoint is /actuator/health
To customize this base path, add the following in application.properties
management.endpoints.web.base-path=/abcd (whatever path is intended)
So, the path for /health will become /abcd/health
How can you disable a particular endpoint provided by actuator?
Add management.endpoints.web.exposure.exclude as key with the endpoint which we want to disable as value in properties file.
What is the use of devtools in spring boot?
Devtools is used in spring boot project to reflect code changes without manual re-start of embedded server. If it is a java code change or configuration change that is whatever is there in classpath, it will restart the server automatically. In case of static content changes, it provides live-reload of browser.
Devtools is a dependency to be added to the pom.xml to use this feature.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.6.7</version>
</dependency>
How server knows what type of data to process?
Using produces and consumes keys under @RequestMapping, we can tell server what type of data, the service is going to produce or what type of data it can consume. We pass the Media type as value as below
@RequestMapping(value = “/orders”, method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
How to run spring boot application on random port?
By default, spring boot application starts on port 8080 which can be changed by mentioning changed port in application.properties or application.yml. To use a random port, we can write server.port=0 in properties file which will make spring boot to choose any random available port.
How to disable a specific auto-configuration in spring boot?
To disable a specific auto-configuration, we need to use exclude tag in @EnableAutoConfiguration or in @SpringBootApplication which in turn uses @EnableAutoConfiguration internally.
Eg. @EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
How to connect spring boot application to external db?
Update application.properties with the driver, username and password of the db using a specific set of keys
Add spring-boot-starter-jpa dependency to use default support of hibernate.
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://……
spring.datasource.username= user
spring.datasource.password=password