Spring REST Interview Questions List in 2020

Today, in the article, we will go through our compiled list of Spring REST interview questions which will benefit Java developers in the interview discussion.

Current article is part of our series on Spring Interview Questions.

Whenever there will be discussion on Spring REST, interviewer first typically starts with questions on RESTful web service as a concept and then moves to the Spring specific questions.

We will do it in same way to give you the maximum benefit.

So, Let’s begin.

spring rest interview questions and answers

Interview Questions on REST Web Service

1. What is REST?

REST stands for Representational State Transfer. It is an architectural style to build web services.

Also, it is an alternate to SOAP based web services.

2. What is RESTful web services?

The web services which adheres to the REST constraints or principles are called as RESTful web services or APIs.

3. What do you mean by REST constraints? Care to explain?

Sure. There are 6 REST architectural constraints:

  1. Client and Server Separation – Client and Server should be able to evolve separately. Meaning, client should not know any back-end logic and server should not know about how UI should interact.
  2. Uniform Interface – A client should have a uniform way to interact with the server. It is the foundation.
    1. Resource Identification – Each server resource should be identifiable in client requests. For example: URI based identification.
    2. Resource Manipulation – Client (with proper permission) should be able to update or delete the resource once it has the whole resource representation.
    3. Self-Descriptive Messages – Client messages should be understandable to the server to process it.
    4. HATEOAS support – Client should be able to interpret relative resource links from a resource.
  3. Statelessness – Every client request should have complete information to process itself. Server failure should not have any impact.
  4. Cacheable – Every server response should explicitly define whether it’s cacheable information.
  5. Layered architecture – Use layered architecture.
  6. Code on demand (Optional) – As part of the representation, a client can ask for “code” which is executable on client side.

4. What purpose does REST based web services serve? Basically, why REST?

All the constraints help build web services which has following characteristics:

  1. Scalability
  2. Has simple and uniform interface to interact with
  3. Performance
  4. Abstraction (Layered)
  5. Component portability (Code on demand)
  6. Interoperability (not REST specific)
  7. Component modifiability feature at run-time

5. What is a REST resource?

Any server response returned to client side in specific format. For example: JSON data.

6. What method does REST supports?

HTTP is the protocol of choice by REST. So, all the HTTP methods are supported by REST.

For example: GET, POST, UPDATE.

7. What are safe REST operations?

HTTP method calls which does not modify anything on the server side, are called as safe REST operations. Basically, all the idempotent methods are safe methods.

For example: GET, HEAD.

8. What are these idempotent and non-idempotent methods?

If for every request, we are creating/adding to the back-end state, then it is non-idempotent.

Otherwise if there is no impact at all or after the first request, then it’s idempotent.

Idempotent methods: GET, PUT[if you are just updating, it will update the same thing], DELETE[once a resource is deleted, won’t have any impact], HEAD, OPTIONS.

Non-Idempotent methods: POST.

Recommended Read: Idempotent REST API

9. Can we build SOAP based web services in Spring?

Yes, we can. Spring even has a separate project for this by the name of Spring Web Services.

This project has multiple out-of-the-box tools and patterns to build SOAP web services.

“Spring specific” Interview Questions on REST

10. How creating RESTful web service is related to Spring MVC?

All the annotations and API which we use to build RESTful web service is part of Spring MVC module.

11. Do you know the Spring MVC jar which needs to be part of project dependency list?

Yes. It’s spring-webmvc.jar

We need to add it in our Maven dependencies like this:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.9.RELEASE</version>
</dependency>

Or if we are using gradle, then like this:

dependencies {
    compile("org.springframework:spring-webmvc:5.2.9.RELEASE")
}

12. Tell me few of the Spring MVC annotations you used while building a REST service?

  1. @RestController – It is the main annotation which marks it as a REST service.
  2. @PathVariable, @RequestBody, @RequestMapping are some of the assistive annotations which help translate an HTTP request to Java.

13. What is the difference between @Controller and @RestController?

@RestController is the combination of @Controller and @ResponseBody. It basically help send the response in JSON format.

14. How to create a RESTful web service in Spring Boot?

Creating REST service in Spring Boot is not way different than Spring MVC.

We just need to add proper dependency and EnableSpringMVC.

15. How to enable Spring MVC in Spring Boot?

If we are using Java configuration in your Spring project, then we just need to annotate our Spring Boot main app or configuration class with @EnableWebMVC and it should be good enough.

15. What is RestTemplate?

RestTemplate is a class which provides methods to communicate with REST services, for our most regular and less frequent use-cases.

It helps us build RESTful web service CONSUMER in a easy way.

16. What is a HttpMessageConverter?

HttpMessageConverter is used to:

  1. Convert incoming HTTP request to Java object(s)
  2. And to convert Java object(s) to outgoing HTTP response

When we @EnableWebMVC, by default we get a handful of such HttpMessageConverters to deal with different types of possible payloads.

Tip: Interviewer can go further on this topic by asking different converters or if you ever added any converter by yourself or customize any. Typically, it doesn’t happen.

17. What is asynchronous REST web service? Do you know about it?

Yes. BUT we go on this route only if our business logic is waiting long on I/O and hence blocking our thread. This is the wastage of our CPU cycles.

18. How to create an asynchronous RESTful web service?

It is just 2 step process:

  1. Add @EnableAsync to our configuration.
  2. Mark the method which we want to execute asynchronously, with @Async.

With this, we are done with our exhaustive list of Spring REST interview questions and answers. This list will for sure help Java developers with varied years of experience in their Java interview discussion.

Best of luck!!

Leave a Comment