Q-1). What is Web Service?

A-1). Web Service is a kind of an application/software that is accessible on Internet. It makes use of the XML Messaging System for both request and response by default.

Q-2). What does REST stand for and what is REST Services?

A-2). REST stands for REpresentational State Transfer. It is an architectural style. In other words we can say that REST is an architectural style to expose web services over HTTP. It sends data from the client to the server using the HTTP protocol.

Q-3). What are the differences between REST and SOAP Web Services?

A-3). The differences between REST and SOAP Web Services are provided below:

  1. REST is simple and easy to use in compare with SOAP
  2. REST is an architectural style, while SOAP is a protocol
  3. REST is a State Transfer, so for that it can use any protocols like HTTP or SOAP. While SOAP is a protocol it only uses one protocol named as Simple Object Access Protocol.
  4. REST supports different formats like JSON, HTML, TEXT, XML while SOAP supports only one format i.e. XML.
  5. REST can be cached to improve performance while SOAP can’t be cached.
Q-4). What is a Resource?

A-4). Any server response that is returned to a client side in any format is called a Resource.

For Example : JSON data or XML Data or HTML Data or Plain Text Data

Q-5). What are safe REST Operations?

A-5). HTTP method calls which doesn’t modify anything on the server side, are called Safe REST Operations.

Example : GET, OPTIONS, HEAD

Q-6). What are idempotent operations?

A-6). There are some HTTP methods like GET that produce the same response no matter how many times you use them. Sending multiple GET requests to the same URI will result in the same response every time. Hence GET method is pure idempotent.

On the other hand, POST method 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 to update the resource. Even multiple PUT requests can be used to update a resource on the server and it will give the same result every time.

PFB the list of HTTP methods:

HTTP MethodIdempotentSafe
OPTIONSYesYes
HEADYesYes
GETYesYes
POSTNoNo
PUTYesNo
DELETEYesNo
PATCHNoNo
Q-7). Is REST scalable/or interoperable?

A-7). Yes. REST is both scalable and interoperable. You can use any technology or language (Java, C++, Python, Ruby, etc.) to write REST services either in client or in server side.

Q-8). Which HTTP methods does REST use?

A-8). REST use any HTTP Methods. But the mostly common used methods are:

  1. GET – For retrieving a resource
  2. POST – For creating a resource
  3. PUT – For updating a resource
  4. DELETE – For removing/deleting a resource
Q-9). Is REST stateless?

A-9). Yes. REST API should be stateless. REST uses HTTP Protocol which is stateless. A request in REST API should contain all the details/data required to process it. It should not rely on session and cookies concept or the previous or next requests.

Q-10). What is the HTTP status return code for a successful DELETE operation?

A-10). There is no hard and fast rule that what code a successful DELETE operation will return. It can return 200 (OK) or 204 (No Content).

In general, if DELETE operation is successful and there is no return body then return 204 (No Content) as code and if there is a return body then return the status code as 200 (OK).

Q-11). What does CRUD mean?

A-11). CRUD means Create, Read, Update and Delete. GET is used to read a Resource, POST is used to create a resource, PUT is used to update a resource and DELETE is used to remove a resource.

Q-12). Is REST secure? What can you do to make it secure?

A-12). REST is normally not secure. But you can introduce security for your REST Endpoints by using Spring-Security. Even you can expose your REST Endpoints in HTTPS, if the underlying server supports HTTPS.

Q-13). Does REST work with Transport Layer Security (TLS)?

A-13). Transport Layer Security (TLS) is used for secure communication between 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.

Q-14). What do you mean by REST constraints?

A-14). There are 6 REST constraints.

  1. Client and Server Separation – Client and Server will be two different entities. it means that Client should not know any back-end logic of Server, while Server should not know about how UI should interact.
  2. Uniform Interface – A Client should have a uniform way to interact with the Server.
    • Resource Identification – Each server resource should be identifiable in Client requests.
    • Resource Manipulation – Client should be able to UPDATE or DELETE a resource once it has the whole resource representation.
    • Self-Descriptive Messages – Client Messages should be understandable to the server to process it.
    • 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.
  4. Cacheable – Every server response should explicitly define whether it’s cacheable information.
  5. Layered Architecture – Use of layered architecture
  6. Code on Demand (Optional) – Client can ask for “code” which is executable on the client side
Q-15). What purpose does REST Service serve?

A-15). The purpose of REST Services are as follows:

  • Scalability
  • Has simple and uniform interface to interact with
  • Performance
  • Abstraction
  • Component Portability (Code on Demand)
  • Interoperability (not REST specific)
  • Component modification feature at run time
Q-16). List few Spring MVC Annotations you used while building a REST Service?

A-16). @RestController – It is the main annotation which makes the service as REST service

@GetMapping, @PutMapping, @PostMapping, @DeleteMapping, @RequestBody, @PathParam, @PathVariable are some of the annotations used in REST Service

Q-17). When do you need @ResponseStatus annotation?

A-17). @ResponseStatus annotation is used during error handling mechanism. Normally, if the API Endpoint server is down then it will throw 500 error status code and if you are requesting the API endpoint with an invalid URL it will throw 400 error status code.

To use it one can create custom exceptions and annotate them using @ResponseStatus annotation and proper HTTP status code and reason.

@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No User Found")  // 404
public class UserNotFoundException extends RuntimeException {
    // ...
}
Q-18). What does @RequestMapping do?

A-18). The @RequestMapping annotation is used to map requests to Spring controller methods. You can map a request based upon HTTP methods like GET, POST, PUT, DELETE.

Nowadays instead of using @RequestMapping annotation, people use @GetMapping for GET HTTP methods, @PostMapping for POST HTTP methods, @PutMapping for PUT HTTP methods and @DeleteMapping for DELETE HTTP methods.

@GetMapping("/allMovies")
public List<Movie> getAllMovies() {
   // ...
}

@PostMapping("/saveMovie")
public List<Movie> saveMovie(@RequestBody Movie movie){
   // ...
}

@PutMapping("/updateMovie/{name}")
public List<Object> updateMovie(@PathVariable(value = "name") String movieName, @RequestBody Movie movie){
   // ...
}

@DeleteMapping("/deleteMovie/{name}")
public List<Object> deleteMovie(@PathVariable(value = "name") String movieName) {
   // ...
}
Q-19). Is @Controller annotation and @RestController annotation both are stereotype?

A-19). Yes, both the @Controller and @RestController annotations are stereotype.

@Controller annotation is actually a specialization of @Component annotation of Spring. It means that Spring Container will automatically detect it.

And @RestController annotation is a specialization of @Controller annotation for the REST Service. It is basically a combination of 2 annotations and they are as @Controller and @ResponseBody annotations.

Q-20). What is the difference between @Controller and @RestController annotations?

A-20). The difference between @Controller and @RestController annotations are as follows:

@Controller@RestController
Specialization of @Component annotationSpecialization of @Controller annotation, combination of @Controller and @ResponseBody annotations
No concept of default type of response generation. You need to specify the MediaTypeBy default generates JSON response
Q-21). Why do you need @ResponseBody annotation?

A-21). The @ResponseBody annotation defined in a method clearly states that the response should be directly written in HTTP Response Body.

@RequestMapping(path = "/hello", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
  return "Hello World";
}

The above “Hello World” output will be directly written in HTTP Response Body. To avoid you can use @RestController annotation, where you don’t have to use @ResponseBody annotation explicitly.

Q-22). What is the use of @PathVariable annotation?

A-22). @PathVariable annotation is used to read values from URI. Like use of Query Parameter in the URI.

@GetMapping("/findMovieByName/{name}")
public Movie getMovieByName(@PathVariable(value = "name") String movieName) {
   // ...
}

Here, you can see {name} is passed as the path variable into the above corresponding URI. And to read that parameter, I have use @PathVariable annotation.

Q-23). What is the use of @EnableWebMVC annotation?

A-23). The @EnableWebMVC annotation is used to enable Spring MVC when you are using Java configuration to configure Spring MVC instead of XML. It is equivalent to the <mvc:annotation-driven> tag used in XML.

Q-24). What is RestTemplate?

A-24). RestTemplate class is an implementation of Template Design Pattern in Spring framework. Similar to the other popular Template class implementation like JdbcTemplate, JmsTemplate, etc. RestTemplate is used to simplify the interactions of REST Services on the client side.

Q-25). What is a HttpMessageConverter?

A-25). HttpMessageConverter is used to:

  1. Convert incoming HTTP request to Java Objects
  2. Also it convert Java Objects to outgoing HTTP Response

@EnableWebMVC annotation, by default provides a handful of such HTTPMessageConverter to deal with different types of possible payloads.

Q-26). What is asynchronous REST Web Service?

A-26). Yes. But we go on this implementation only if our business logic is waiting long on I/O and hence blocking the thread. This is the wastage of our CPU cycles.

Q-27). How to create an asynchronous REST Service?

A-27). There are just 2 steps required to make a REST Service asynchronous. They are as follows:

  1. Use of @EnableSync annotation to configuration
  2. Mark the method with @Asynch annotation for executing it asynchronously.