Spring Boot Microservice Interview Questions & Answers

In this article, we will discuss some of the frequently asked Spring Boot Microservice interview questions and answers.

For generic Microservice interview questions and answers, refer the following post :
10+ Must Read Microservice Interview Questions
 

Spring Boot Microservice Interview Questions

Q 1 : What is Spring Boot? Why is Spring Boot popular for Java microservice development ?

Spring Boot provides a lot of boiler plate code and configurations that we were manually doing for application development.
It abstracts the maven dependencies, configurations, application server and reduces development time.
Spring Boot makes it easier to create production ready applications in no time.

Since it follows convention over configuration, it comes with default configurations for most of the Spring projects including microservice development.
So, we don’t need to do much to bootstrap the spring boot microservice starter project.

 

Q 2 : What are the advantages of Spring Boot ?

Spring Boot is popular for microservice development because of following advantages:

– Reduces boiler plate code
– Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
– Provide production-ready features such as metrics, health checks and externalized configuration
– Provide opinionated ‘starter’ dependencies to simplify your build configuration
– Automatically configure Spring and 3rd party libraries whenever possible
– Provide production-ready features such as metrics, health checks and externalized configuration
– Absolutely no code generation and no requirement for XML configuration
 

Q 3: What is YAML ?

YAML is a convenient syntax for storing external properties in a hierarchical format.

We can also use application.yaml or application.yml file as an alternative to application.properties.

For example, lets consider the following property in application.properties :


spring.application.name=topjavatutorial

The same can be written in application.yaml as :


spring:
  application:
    name: topjavatutorial

 

Q 4: How to configure the port for Spring boot application ?

In order to run a spring boot application on a custom port, you can do the following :

– Set the port as a command line parameter


-Dserver.port=8090 or --server.port=8090

– Set the sever.port in application.properties.


server.port=8090

– Set the port in application.yml


server:
    port: 9999

Note: If we set the port to 0, random ports will be assigned.

 

Q 5: What is Swagger ? How to integrate it with Spring Boot ?

Swagger is set of open source tools that helps with creating documentation for your REST services.

Refer this article for integrating Swagger with Spring Boot:

How to integrate Swagger with Spring Boot

 

Q 6: How to add a context path to Spring Boot application ?

By default, the context path is “/”.

We can update the contextPath in application.properties file.

For example, if we are trying to add contextPath as “/mycontext”, we can add it as :


server.contextPath=/mycontext

With Spring Boot 2.0, the same can be added as :


server.servlet.contextPath=/mycontext

 

Q 7 : How to set the logging level with application.properties?

We can add the log levels TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF in application.properties.

The syntax is :


logging.level.<logger-name>=<level>

The root logger can be configured by using logging.level.root.

For example,


logging.level.root=WARN
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

 

Q 8 : What are Spring boot Starter POMs ?

Starter POMs are convinient descriptors that you can add in your pom.xml. They contain lot of dependencies that help you setup and run your project quickly.

They follow naming convention spring-boot-starter-*, where * is a particular type of application.

Here are some examples :

spring-boot-starter is the core Spring Boot starter, including auto-configuration support, logging and YAML.

Similarly, spring-boot-starter-web has Tomcat and spring-webmvc and you can add this to create Spring MVC application.

 

Q 9 : What is actuator in Spring boot?

Actuators allow you to monitor and interact with your application.

Spring Boot includes a number of built-in endpoints and you can also add your own.

For example, the “health” endpoint provides basic application health information. Similarly, we can use “metrics” too get metrics information for the current application.

To enable actuators, add a dependency to the spring-boot-starter-actuator Starter POM.

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

 

Q 10 : How to access a value defined in the application.properties file in Spring Boot ?

We can access properties defined in application.properties file inside the our bean using the @Value tag.

For example, the following will read a property “name” from the application.properties :

import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}

 

Q 11 : How to log sql queries and values in Spring Boot Hibernate application ?

To log the queries, add the following properties in application.properties :

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true

To log the values, add the following as well :
spring.jpa.properties.hibernate.type=trace 

 

© 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags