REST and Spring MVC Interview Questions For Java Developers
REST and Spring MVC Interview Questions For Java Developers
REST stands for the REpresentational State Transfer, which uses the HTTP
protocol to send data from the client to the server, e.g. a book in the server
can be delivered to the client using JSON or XML. However, if you are not
familiar with REST.
What is a resource?
A resource is how data is represented in the REST architecture. By exposing
entities as the resource, it allows a client to read, write, modify, and create
resources using HTTP methods, for example, GET, POST, PUT, DELETE, etc.
What are safe REST operations?
REST API uses HTTP methods to perform operations. Some of the HTTP
operations, which doesn't modify the resource at the server, are known as
safe operations, including GET and HEAD. On the other hand, PUT, POST,
and DELETE are unsafe, because they modify the resource on the server.
What are idempotent operations? Why is idempotency important?
There are some HTTP methods — like GET — that produce the same
response no matter how many times you use them, sending multiple GET
request to the same URI will result in same response without any side-
effect. Hence, this is known as idempotent.
On the other hand, the POST is not idempotent, because if you send
multiple POST request, 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 can be used to update a resource on a server
and will give the same end result. You can take a HTTP
Fundamentals course by Pluralsight to learn more about idempotent
methods of HTTP protocol and HTTP in general.
What are the advantages of the RestTemplate?
The RestTemplate class is an implementation of the Template method
pattern in the Spring framework. Similar to other popular template classes,
like the JdbcTemplate or JmsTempalte, it also simplifies the interaction with
RESTful web services on the client side. You can use it to consume a
RESTful web servicer very easily, as shown in this RestTemplate example.
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
resource, and DELETE for removing a resource from the server.
What is an HttpMessageConverter in Spring REST?
An HttpMessageConverter is a strategy interface that specifies a converter that
can convert from and to HTTP requests and responses. Spring REST uses
this interface to convert HTTP responses to various formats, for example,
JSON or XML.
Each HttpMessageConverter implementation has one or several MIME Types
associated with it. Spring uses the "Accept" header to determine the
content type that the client is expecting.
It will then try to find a registered HTTPMessageConverter that is capable
of handling that specific content-type and use it to convert the response
into that format before sending it to the client. If you are new to Spring
MVC.
How to create a custom implementation of the HttpMessageConverter
to support a new type of request/responses?
You just need to create an implementation of
the AbstractHttpMessageConverter and register it using
the WebMvcConfigurerAdapter#extendMessageConverters() method with the
classes that generate a new type of request/response.
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
to process it. It should not rely on previous or next requests or some data
maintained at the server end, like sessions. The REST specification puts a
constraint to make it stateless, and you should keep that in mind while
designing your REST API.
What does @RequestMapping annotation do?
The @RequestMapping annotation is used to map web requests to Spring
Controller methods. You can map a request based upon HTTP methods, e.g.
GET, POST, and various other parameters.
For example, if you are developing a RESTful web service using Spring, then
you can use, produce, and consume property along with media type
annotations to indicate that this method is only used to produce or
consume JSON, as shown below:
@RequestMapping (method = RequestMethod.POST, consumes="application/json")
public Book save(@RequestBody Book aBook) {
return bookRepository.save(aBook);
}
Similarly, you can create other handler methods to produce JSON or XML. If
you are not familiar with these annotations, then I suggest you join
this Spring MVC For Beginners course on Udemy to learn the basics.
Is @Controller a stereotype? Is @RestController a stereotype?
Yes, both @Controller and @RestController are stereotypes. The @Controller is
actually a specialization of Spring's @Component stereotype annotation. This
means that the class annotated with the @Controller will also be
automatically detected by the Spring container, as part of the container's
component scanning process.
And, the @RestController is a specialization of the @Controller for the RESTful
web service. It not only combines
the @ResponseBody and @Controller annotations, but it also gives more
meaning to your controller class to clearly indicate that it deals with
RESTful requests.
Your Spring Framework may also use this annotation to provide some
more useful features related to REST API development in future.
If this exception is thrown from any handler method, then the HTTP error
code 404 with the reason "No such Book" will be returned to the client.
If you are not familiar with the basics concepts of Spring MVC, Security, and
REST, I suggest you go through these REST with Spring and Learn Spring
Security courses to gain some experience before your next job interview.
These two courses are specially designed to provide you with some real-
world experience to boost both your knowledge and experience with
Spring MVC, REST, and Spring Security.
Is REST secure? What can you do to secure it?
REST and Spring Security is a broad term; it could mean security of
message, which is provided by encryption or access restriction that are
provided using authentication and authorization. REST is normally not
secure, but you can secure it by using Spring Security.
At the very least, you can enable the HTTP basic authentication by using
HTTP in your Spring Security configuration file. Similarly, you can expose
your REST API using HTTPS, if the underlying server supports HTTPS.
Does REST work with transport layer security (TLS)?
Transport Layer Security (TLS) is used for secure communication between
the client and server. It is the successor of SSL (Secure Socket Layer). Since
HTTPS can work with both SSL and TLS, REST can also work with TLS.
Actually, in REST, it is up to the server to implement security protocols. The
same RESTful web service can be accessed using HTTP and HTTPS, if the
server supports SSL.
If you are using Tomcat, you can learn more about how to enable SSL in
Tomcat.
Do you need Spring MVC in your classpath for developing RESTful
Web Service?
It's actually Spring MVC that provides all useful annotations, like
@RestController, @ResponseCode , @ResponseBody, @RequestBody,
and @PathVariable (see REST with Spring). Hence, you must use spring-
mvc.jar or the appropriate Maven entry in your pom.xml