Flat Preloader Icon

Spring Boot Interviews Programmes

1. Explain what is thymeleaf and how to use thymeleaf ?

Answer: Thymeleaf is a server-side Java template engine used for web applications. It aims to bring natural template for your web application and can integrate well with Spring Framework and HTML5 Java web applications. To use Thymeleaf, you need to add the following code in the pom.xml file:


2. Mention the steps to connect Spring Boot application to a database using JDBC ?

Answer: Spring Boot starter projects provide the required libraries to connect the application with JDBC. So, for example, if you just have to create an application  and connect it withmMySQL database, you can follow the below steps:

Step 1: Create a database in MySQL

					CREATE DATABASE example;

Step 2: Then you have to create a table inside this database.

					CREATE TABLE customers(customerid INT PRIMARY 
customername VARCHAR(255));  


Step 3: Now, create a Spring Boot project and provide the required details

Step 4: Add the JDBC, MySQL and web dependencies.

Step 5: Once the project is created, you have to configure the database into application properties


Step 6: The main application.java class should have the following code:

					package com.edureka;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot
public class SampleApplication {  
    public static void main(String[] args) {  
        (SampleApplication.class, args);  

Step 7: Next, you have to create a controller to handle the HTTP requests, by mentioning the following code:

					package com.edureka;
import org.springframework.web
import org.springframework
import org.springframework
import org.springframework.web.bind
public class JdbcController {
JdbcTemplate jdbc;
public String index(){
jdbc.execute("insert into
return "Data Entry Successful";

Step 8: Finally, execute this project as a Java application.
Step 9: Next, open the URL (localhost:8080/insert), and you will see the output as Data Entry Successful. You can also go forward and check if the data is entered into the table.

3. What do you understand by Spring Data REST ?

Answer: Spring Data REST is used to expose the RESTful resources around Spring Data repositories. Consider the following example:

= "sample", path
= "sample")
public interface SampleRepository 
extends CustomerRepository<sample, Long> {

Now, to expose the REST services, you can use the POST method in the following way:

"customername": "Rohit"

Response Content

4. What are the @RequastMaping and @RestContoler annotation in Spring Boot used for ?

Answer: The @RequestMapping annotation can be used at class-level or method level in your controller class.

The global request path that needs to be mapped on a controller class can be done by using @RequestMapping at class-level. If you need to map a particular request specifically to some method level.

Below is a simple example to refer to:

public class GreatLearningController {
String greatLearning(){
return "Hello from greatLearning ";
String welcome(){
return "Welcome from GreatLearning";

The @RestController annotation is used at the class level.

You can use @RestController when you need to use that class as a request handler class.All the requests can be mapped and handled in this class.

@RestController itself consists @Controller and @ResponseBody which helps us to remove the need of annotating every method with @ResponseBody annotation.

Below is a simple example to refer to for use of @RestController annotation:

public class DemoRestController{
@GetMapping(“/{id}”,produces =”application/json”)
public Bank getBankDetails(@PathVariable int id){
return findBankDetailsById();

Here, @ResponseBody is not required as the class is annotated with @RestController.

5. How to insert data in mysql using spring boot ?

Answer: First configure mysql in your spring boot application.

Then you can map your entities with your db tables using JPA.

And with the help of save() method in JPA, you can directly insert your data into your database.

public class Controller {
private final GreatLearningRepository
public Controller(GreatLearningRepository
greatLearningRepository) {
this. greatLearningRepository =

In the above case, your data which may be in JSON format can be inserted successfully into the database.

					@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> insert(@RequestBody Course course) {
return ResponseEntity.accepted().build();

6. How to handle 404 error in Spring Boot ?

Answer: Consider a scenario, where there are no stockDetails in the DB and still, whenever you hit the GET method you get 200(OK) even though the resource is not found which is not expected. Instead of 200, you should get 404 error.
So to handle this, you need to create an exception, in the above scenario “StockNotFoundException”.

public Stock retriveStock
(@PathVariable int number)  
Stock  stock  = service.findOne(number);  
if(Stock  ==null)  
//runtime exception  
throw new StockNotFoundException("number: "+ number);  
return stock;  

Now, create a Constructor from Superclass.

Right-click on the file -> Go to Source ->And generate constuctors from superclass-> and check the RuntimeException(String)-> and generate.

And add an annotation called @ResponseStatus which will give you 404 (not found) error.

					package com.greatlearning;  
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation
public class StockNotFoundException extends 
public StockNotFoundException(String message)   

Now, you can hit the same URL again and there you go, you get a 404 error when a resource is not found.

7. How to Configure Spring Boot Application logging ?

Answer: Spring Boot provides a LoggingSystem abstraction that configures logging based on the content of the classpath. If Logback is available, it is definitely the first choice.

Suppose the only change the user needs to make to logging is to set the levels of various loggers. In that case, they can do so in application.properties by using the “logging.level” prefix, as shown in the following example:


8. How Can You Change the Default Port in Spring Boot ?

Answer: Default port is 8080; The user can change the default port by:

  1. Command-line:
					java -jar spring-5.jar --server.port=8083
  1. By changing in application.properties file
  1. Programmatic Configuration:
public class CustomApplication {
public static void main(String[] args {
SpringApplication app = new SpringApplication
("server.port", "8083"));

9. How does a Spring Boot application get started ?

Answer:  Just like any other Java program, a Spring Boot application must have a main method. This method serves as an entry point, which invokes the SpringApplication#run method to bootstrap the application.

public class MyApplication { 
public static void main(String[] args) {    
               // other statements     


10. What are starter dependencies ?

Answer: Spring boot starter is a maven template that contains a collection of all the relevant transitive dependencies that are needed to start a particular functionality.
Like we need to import spring-boot-starter-web dependency for creating a web application.

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