Spring REST Web Services Interview Questions and Answers

Spring REST Web Services Interview Questions and Answers

In this tutorial, I am putting collection of frequently asked important spring REST web services interview questions with answers. It will be help in the Spring REST related questions in the interview. You could also read my another articles related to Spring interview questions with answers for helping in the Spring related interviews. These articles listed as below.

 

Let’s see the below questions related to Spring REST web services.

Spring REST Service Interview Questions

  1. Define Web Service?
  2. What does REST stand for? and what is RESTful web services?
  3. What is differences between RESTful web services and SOAP web services?
  4. What is a resource?
  5. What are safe REST operations?
  6. What are idempotent operations? Why is idempotency important?
  7. Is REST scalable and/or interoperable?
  8. What are the advantages of the RestTemplate?
  9. Which HTTP methods does REST use?
  10. What is an HttpMessageConverter?
  11. Is REST normally stateless?
  12. What does @RequestMapping do?
  13. Is @Controller a stereotype? Is @RestController a stereotype?
  14. What is the difference between @Controller and @RestController?
  15. When do you need @ResponseBody?
  16. What does @PathVariable do?
  17. What is the HTTP status return code for a successful DELETE statement?
  18. What does CRUD mean?
  19. Is REST secure? What can you do to secure it?
  20. Where do you need @EnableWebMVC?
  21. Name some common http response codes. When do you need @ResponseStatus?
  22. Does REST work with transport layer security (TLS)?
  23. Do you need Spring MVC in your classpath for Spring REST?

1. Define Web Service?
A web service is a kind of software that is accessible on the Internet. It makes use of the XML messaging system and offers an easy to understand, interface for the end users.

2. What does REST stand for? and what is RESTful web services?
REST stand for REpresentational State Transfer, term by Roy Fielding. REST is an architectural style that describes best practices to expose web services over HTTP with Emphasizes scalability. Web services written by apply REST Architectural concept are called RESTful web services which focus on System resources and how state of Resource should be transferred over http protocol to a different clients written in different languages. In RESTful web services http methods like GET, PUT, POST and DELETE can can be used to perform CRUD operations. Read more about REST.

3. What is differences between RESTful web services and SOAP web services?
1) REST is more simple and easy to use than SOAP
2) REST uses HTTP protocol for producing or consuming web services while SOAP uses XML.
3) REST is lightweight as compared to SOAP and preferred choice in mobile devices and PDA’s.
4) REST supports different format like text, JSON and XML while SOAP only support XML.
5) REST web services call can be cached to improve performance.

4. What is a resource?
The key abstraction of information in REST is a resource. Any information that can be named can be a resource like Student, Employee etc. REST is resource based API. Expose resources through URIs like http://localhost:8080/api/employees/1111, It is based on Model nouns, not verbs. Resources support limited set of operations – GET, PUT, POST, DELETE in case of HTTP. Example: update an employee use PUT to /employees/1111 and don’t POST to /employees/edit?id=1111 and Resources can support multiple representations like HTML, XML, JSON etc. Multiple URIs may refers to same resource as like CRUD operation on employee resource using HTTP verbs. Read More>>>

5. What are safe REST operations?
Safe REST operations are HTTP methods that do not modify resources. For example, using GET or HEAD on a resource URL, should NEVER change the resource. But it is still possible, that safe operations do change things on a server or resource. For example: GET http://localhost:8080/api/employees/1111/delete HTTP/1.1 but it is incorrect, this would actually delete the employee i.e. Safe methods are methods that can be cached, prefetched without any repercussions to the resource.

6. What are idempotent operations? Why is idempotency important?
Idempotent operations are those HTTP methods that can be called repetitively many times without different outcomes. It would not matter if the method is called only once, or many times over. The result should be the same. Again, this only applies to the result, not the resource itself.
Consider the following examples:

a = 4; (is idempotent)
a++; (is not idempotent)

Idempotency is important in building a fault-tolerant API. Suppose a client wants to update a resource through POST. Since POST is not a idempotent method, calling it multiple times can result in wrong updates. What would happen if you sent out the POST request to the server, but you get a timeout. Is the resource actually updated? Does the timeout happened during sending the request to the server, or the response to the client? Can we safely retry again, or do we need to figure out first what has happened with the resource? By using idempotent methods, we do not have to answer this question, but we can safely resend the request until we actually get a response back from the server.

HTTP Method Idempotent Safe
OPTIONS yes yes
GET yes yes
HEAD yes yes
PUT yes no
POST no no
DELETE yes no
PATCH no no

7. Is REST scalable and/or interoperable?
Yes, REST is scalable and interoperable.

8. What is the RestTemplate and It’s advantages?
In Spring REST client, The RestTemplate is the core class for client-side access to Spring RESTful web services. It communicates HTTP server using RESTful constraints. It is very similar to other template classes in the Spring like JdbcTemplate and HibernateTemplate etc. In Spring, RestTemplate provides higher level implementation of corresponding HTTP methods such as GET, POST, PUT, DELETE, HEAD etc. It provides the methods to communicate by using these HTTP methods with URI template, URI params, request object and response type as arguments. Read More about RestTemplate.

9. Which HTTP methods does REST use?
RestTemplate provides higher level implementation of corresponding HTTP methods such as GET, POST, PUT, DELETE, HEAD etc.

10. What is an HttpMessageConverter?
HttpMessageConverter is a strategy interface that specifies a converter that can convert from and to HTTP requests and responses. Internally Spring MVC uses it to convert the Http request to an object representation and back. Message converters are used to marshall and unmarshall Java Objects to and from JSON, XML, etc – over HTTP. Read More about HttpMessageConverter in Spring.

11. Is REST normally stateless?
Yes.

12. What does @RequestMapping do?
@RequestMapping can be applied to the controller class as well as methods. This annotation for mapping web requests onto specific handler classes and/or handler methods. Read more about @RequestMapping annotation.

13. Is @Controller a stereotype? Is @RestController a stereotype?
Yes.

14. What is the difference between @Controller and @RestController?
The key difference between a traditional Spring MVC @Controller and the RESTful web service @RestController is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML by registered HttpMessageConverters.

15. When do you need @ResponseBody?
When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the http response automatically. Each method in the Controller class must be annotated with @ResponseBody.

16. What does @PathVariable do?
@PathVariable is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose @PathVariable annotation is used in Spring mvc framework. Read More about PathVariable.

17. What is the HTTP status return code for a successful DELETE statement?
Either we can return HTTP status 204 (NO CONTENT) with no response body or return HTTP status 200 (OK) along with a response body with representation of the deleted item.

18. What does CRUD mean?
In Spring REST, the primary or most-commonly-used HTTP verbs are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively.

19. Is REST secure? What can you do to secure it?
REST is nothing but it architectural desgin or paradigms to design restful web service. So REST does not provide any security constraints by default but we can implement it in the RESTful web services by using security layers like transport layer security (TLS), auth token security.

20. Where do you need @EnableWebMVC?
@EnableWebMvc- It used for enabling spring mvc behavior i.e. it is responsible for registering Spring MVC required Bean Post Processors in the application as like Jackson2JsonMessageConverter etc.

21. Name some common http response codes. When do you need @ResponseStatus?

  • 2xx Success
    • 200 OK
    • 201 Created
    • 204 No Content
  • 3xx Redirection
    • 304 Not Modified
  • 4xx Client Error
    • 400 Bad Request
    • 401 Unauthorized
    • 403 Forbidden
    • 404 Not Found
    • 409 Conflict
  • 5xx Server Error
    • 500 Internal Server Error
    • 503 Service Unavailable

@ResponseStatus annotation available on custom exceptions and to map these exceptions to HTTP status codes.

22. Does REST work with transport layer security (TLS)?
Yes. Transport Layer Security can encrypt the communication to a RESTful Webservice and authenticate the server to a client.

23. Do you need Spring MVC in your classpath for Spring REST?
Yes, Spring REST uses the Spring MVC annotations like @RestController, @RequestBody and @ResponseBody etc.

 

Previous