0% found this document useful (0 votes)
2 views

Spring Boot Interview Q &A

The document provides an overview of REST architecture, explaining key concepts such as resources, safe and idempotent operations, and HTTP methods used in REST APIs. It also covers Spring-specific annotations like @RequestMapping, @RequestParam, and @PathVariable, as well as Spring Boot features like Spring Initializer, Spring Actuator, and Spring Boot CLI. Additionally, it discusses the differences between embedded containers and WAR files in Spring Boot applications.

Uploaded by

Harshal More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Spring Boot Interview Q &A

The document provides an overview of REST architecture, explaining key concepts such as resources, safe and idempotent operations, and HTTP methods used in REST APIs. It also covers Spring-specific annotations like @RequestMapping, @RequestParam, and @PathVariable, as well as Spring Boot features like Spring Initializer, Spring Actuator, and Spring Boot CLI. Additionally, it discusses the differences between embedded containers and WAR files in Spring Boot applications.

Uploaded by

Harshal More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What does REST stand for?

REST stands for REpresentational State Transfer, which uses HTTP protocol to send data from client
to server e.g. a book in the server can be delivered to the client using JSON or XML.

2. What is a resource?

A resource is how data is represented in REST architecture. By exposing entities as the resource it
allows a client to read, write, modify, and create resources using HTTP methods like GET, POST, PUT,
DELETE, etc.

3. What are safe REST operations?

REST API uses HTTP methods to perform operations. Some of the HTTP operations which don't
modify the resource at the server are known as safe operations e.g. GET and HEAD. On the other
hand, PUT, POST, and DELETE are unsafe because they modify the resource on the server.

4. What are idempotent operations? Why is idempotency important?

There are some HTTP methods e.g. GET which produce same response no matter how many times
you use them e.g. sending multiple GET request to the same URI will result in same response without
any side-effect hence it is known as idempotent.

On the other hand, the POST is not idempotent because if you send multiple POST requests, it will
result in multiple resource creation on the server, but again, PUT is idempotent if you are using it to
update the resource. Even, multiple PUT requests to update a resource on a server will give the
same end result.

5. Which HTTP methods does REST use?

REST can use any HTTP methods but the most popular ones are GET for retrieving a resource, POST
for creating a resource, PUT for updating the resource and DELETE for removing a resource from the
server.

6. Is REST normally stateless?

Yes, REST API should be stateless because it is based on HTTP which is also stateless. A Request in
REST API should contain all the details required it to process i.e. it should not rely on previous or
next request or some data maintained at the server end e.g. Sessions. REST specification puts a
constraint to make it stateless and you should keep that in mind while designing your REST API.

7. What does @RequestMapping annotation do?

The @RequestMapping annotation is used to map web requests to Spring Controller methods. You
can map requests based upon HTTP methods like the GET and POST and various other parameters.
For examples, if you are developing RESTful Web Service using Spring then you can use produces and
consumes property along with media type annotation to indicate that this method is only used to
produce or consumers JSON as shown below:

@RequestMapping (method = RequestMethod.POST, consumes="application/json")

public Book save(@RequestBody Book aBook) {

return bookRepository.save(aBook);

8. What is the difference between @Controller and @RestController.

@Controller @RestController

@RestController annotation is a special controller used in


@Controller is used to mark classes
RESTful Web services, and it’s the combination of
as Spring MVC Controller.
@Controller and @ResponseBody annotation.

It is a specialized version of
It is a specialized version of @Controller annotation.
@Component annotation.

In @Controller, we can return a


In @RestController, we can not return a view.
view in Spring Web MVC.

@Controller annotation indicates @RestController annotation indicates that class is a


that the class is a “controller” like a controller where @RequestMapping methods assume
web controller. @ResponseBody semantics by default.

In @Controller, we need to use


In @RestController, we don’t need to use
@ResponseBody on every handler
@ResponseBody on every handler method.
method.

It was added to Spring 2.5 version. It was added to Spring 4.0 version.
9. What is @RequestParam?

The @RequestParam annotation is used to extract query parameters from the URL. Query
parameters are found after the "?" symbol in the URL and are separated by "&".

For example, consider the URL:

http://localhost:8080/api/users?userId=123

Here, userId=123 is a query parameter.

@RequestMapping("/api/users")
public ResponseEntity<String> getUser(@RequestParam("userId") String userId) {
// code to fetch the user with the given userId
}

10. What is @PathVariable?

The @PathVariable annotation is used to extract path variables from the URL. Path variables are
part of the URL itself, not the query parameters.

For example, consider the URL:

http://localhost:8080/api/users/123

Here, 123 is a path variable, representing the user ID.

@RequestMapping("/api/users/{userId}")
public ResponseEntity<String> getUser(@PathVariable("userId") String userId) {
// code to fetch the user with the given userId
}

--------------------------------------------

@RequestParam: Handles query parameters.

Example: http://localhost:8080/api/users?userId=123

@PathVariable: Handles path variables.

Example: http://localhost:8080/api/users/123

------------------------------------------------

11. What is the difference between @SpringBootApplication and @EnableAutoConfiguration


annotation?

Even though both are essential Spring Boot application and used in the Main class or Bootstrap class
there is a subtle difference between them. The @EnableAutoConfiguration is used to enable auto-
configuration but @SpringBootApplication does a lot more than that.

It also combines @Configuration and @ComponentScan annotations to enable Java-based


configuration and component scanning in your project.
The @SpringBootApplication is in fact combination of @Configuration, @ComponentScan and
@EnableAutoConfiguration annotations.

12. What is Spring Initializer? why should you use it?

One of the difficult things to start with a framework is initial setup, particularly if you are starting
from scratch and you don't have a reference setup or project. Spring Initializer addresses this
problem in Spring Boot.

It's nothing but a web application which helps you to create initial Spring boot project structure and
provides Maven or Gradle build file to build your code.it is highly recommend using it if you are
starting the first time.

13. What is Spring Actuator? What are its advantages?

Spring Actuator is another cool Spring Boot feature which allows seeing inside a running application.

It allows you to see inside an application. Since Spring Boot is all about auto-configuration it makes
debugging difficult and at some point in time, you want to know which beans are created in Spring's
Application Context and how Controllers are mapped. Spring Actuator provides all that information.

It provides several endpoints e.g. a REST endpoint to retrieve this kind of information over the web.
It also provides a lot of insight and metrics about application health e.g. CPU and memory usage,
number of threads etc.

It also comes with a remote shell which you can use to securely go inside Spring Boot application and
run some command to expose the same set of data. You can even use JMX to control this behavior
at runtime.

Btw, it's important to secure your Spring Actuator endpoints because it exposes a lot of confidential
information and a potentially dangerous one-two. For example, by using /showdown endpoint you
can kill a Spring Boot application.

14. What is Spring Boot CLI? What are its benefits?

Spring Boot CLI is a command-line interface which allows you to create Spring-based Java application
using Groovy. Since it's used Groovy, it allows you to create Spring Boot application from the
command line without ceremony e.g. you don't need to define getter and setter method, or access
modifiers, return statements etc.

It's also very powerful and can auto-include a lot of library in Groovy's default package if you happen
to use it.
15. Where do you define properties in Spring Boot application?

You can define both application and Spring boot related properties into a file called
application.properties. You can create this file manually or you can use Spring Initializer to create
this file, albeit empty.

You don't need to do any special configuration to instruct Spring Boot load this file. If it exists in
classpath then Spring Boot automatically loads it and configure itself and application code according.

16. Can you change the port of the Embedded Tomcat server in Spring boot? If Yes, How?

Yes, we can change the port of Embedded Tomcat Server in Spring Boot by adding a property called
server.port in the application.properties file.

17. What is the difference between an embedded container and a WAR?

The main difference between an embedded container and a WAR file is that you can Spring Boot
application as a JAR from the command prompt without setting up a web server. But to run a WAR
file, you need to first set up a web server like Tomcat, which has a Servlet container, and then you
need to deploy WAR there.

18. What embedded containers does Spring Boot support?

Spring Boot supports three embedded containers: Tomcat, Jetty, and Undertow. By default, it uses
Tomcat as embedded containers, but you can change it to Jetty or Undertow.

You might also like