20 Spring REST Web Service Interview Questions: What Does REST Stand For?
20 Spring REST Web Service Interview Questions: What Does REST Stand For?
com/articles/top-20-spring-mvc-interview-questions-answers
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, I suggest you to first check out the REST API design and
development to better understand it.
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.
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.
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.
Yes, REST is scalable and interoperable. It doesn't mandate a specific choice of technology either at client
or server end. You can use Java, C++, Python, or JavaScript to create RESTful web services and consume
them at the client end. I suggest you read a good book on REST API, like RESTful Web Services to learn
more about REST.
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.
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.
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.
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:
1
@RequestMapping (method = RequestMethod.POST, consumes="application/json")
2
public Book save(@RequestBody Book aBook) {
3
return bookRepository.save(aBook);
4
}
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? (answer)
Your Spring Framework may also use this annotation to provide some more useful features related to
REST API development in future.
This makes the development of RESTful web services easier using Spring. You can see here to learn more
about Spring Boot and how it can help you to create Spring MVC based web applications.
The @ResponseBody annotation can be put on a method to indicate that the return type should be
written directly to the HTTP response body (and not placed in a Model, or interpreted as a view name).
For example:
1
@RequestMapping(path = "/hello", method = RequestMethod.PUT)
2
3
@ResponseBody
4
5
public String helloWorld() {
6
7
return "Hello World";
8
9
}
What does @PathVariable do in Spring MVC? Why it's useful in REST with Spring? (answer)
This is one of the useful annotations from Spring MVC that allows you to read values from the URI, like
query parameter. It's particularly useful in case of creating RESTful web service using Spring, because, in
REST, resource identifiers are part of the URI. This question is normally asked by experienced Spring
MVC developers with 4 to 6 years of experience.
For example, this URL can be helpful if you want to learn how to extract the id, then you can use
the @PathVariable annotation of Spring MVC. If you are not familiar with Spring MVC annotations,
then Spring MVC For Beginners: Build Java Web App in 25 Steps is a good place to start.
What is the HTTP status return code for a successful DELETE statement? (answer)
There is no strict rule about what status code your REST API should return to after a successful DELETE.
It can return 200 Ok or 204 No Content.
In general, if the DELETE operation is successful, the response body is empty, return 204. If the DELETE
request is successful and the response body is NOT empty, return 200.
CRUD is a short form of Create, Read, Update, and Delete. In REST API, the POST is used to create a
resource, GET is used to read a resource, PUT is used to updated a resource, and DELETE is used to
remove a resource from the server. This one is another beginner level Spring MVC question common
amongst 1 to 3 years as an experienced programmer.
This may work for a human user but not for REST clients. You need to send them the proper status code,
like 404, if the resource is not found. That's where you can use the @ResponseStatus annotation,
which allows you to send custom HTTP status codes along with proper error message in case of an
exception.
In order to use it, you can create custom exceptions and annotate them using
the @ResponseStatus annotation and proper HTTP status code and reason.
When such exceptions are thrown from the controller's handler methods and not handled anywhere else,
then the appropriate HTTP response with the proper HTTP status code is sent to the client.
For example, if you are writing a RESTful web service for a library that provides book information, then
you can use @ResponseStatus to create an exception that returns the HTTP response code 404 when a
book is not found instead of the Internal Server Error (500), as shown below:
1
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Book") // 404
2
3
public class BookNotFoundException extends RuntimeException {
4
5
// ...
6
7
}
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.
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.
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? (answer)
This question is often asked by Java programmers with 1 to 2 years of experience in Spring. The short
answer is: yes — you need Spring MVC in your Java application's classpath to develop RESTful web
services using the Spring framework.
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
That's all for now about some of the frequently asked Spring REST interview questions for beginners
and experienced Java JEE developers. These questions are also very useful to brush up your knowledge
about Spring REST, if you are going to take Pivotal's Spring Certification.
If are already preparing for your Spring Developer certification, and you need more such questions from
the Spring certification perspective, you will find a lot of questions on this topic on David Mayer's Core
Spring Simulator, one of the best simulators to pass the Spring certification at the moment. Good luck on
your interviews!