Spring Boot Interview Q &A
Spring Boot Interview Q &A
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.
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.
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.
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.
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.
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:
return bookRepository.save(aBook);
@Controller @RestController
It is a specialized version of
It is a specialized version of @Controller annotation.
@Component annotation.
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 "&".
http://localhost:8080/api/users?userId=123
@RequestMapping("/api/users")
public ResponseEntity<String> getUser(@RequestParam("userId") String userId) {
// code to fetch the user with the given userId
}
The @PathVariable annotation is used to extract path variables from the URL. Path variables are
part of the URL itself, not the query parameters.
http://localhost:8080/api/users/123
@RequestMapping("/api/users/{userId}")
public ResponseEntity<String> getUser(@PathVariable("userId") String userId) {
// code to fetch the user with the given userId
}
--------------------------------------------
Example: http://localhost:8080/api/users?userId=123
Example: http://localhost:8080/api/users/123
------------------------------------------------
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.
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.
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.
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.
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.
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.