Lifecycle of a request and response process for a SPRING REST API

Developing a REST API using Spring Boot framework accelerates the development process, and allows API developer to only focus on writing the core business logic. However understanding the end to end  a request life cycle will help the developers to understand the overall process and behind the scene activities done by Spring, to debug and trouble shoot issues.

In order to create a REST API to serve a client with a list of users fetched from a database, the tasks involved are

  • Create a class with @RestController annotation. Due to the annotation this class will be auto detected through class path scanning and the methods annotated with @RequestMapping annotation will be exposed as HTTP endpoints. when an incoming request matches the requirements specified by the @RequestMapping annotation, the method will execute to serve the request.

For our example of users API , the controller class will look like this


@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserService userService
@RequestMapping(method = RequestMethod.GET)
public List findAllUsers() {
return userService.findAllUsers();

}
}

An incoming GET request for /users will execute the findAllUsers methods

  • Create a class for business logic
  • Create a class to fetch data from the user table

From a developer’s perspective the flow to fetch the list of users from the database can be viewed as below

Capture

However, with spring doing lot of work for us behind the scenes, the lifecycle of the entire process from making a HTTP request to a resource to serving the response back to client in either XML/JSON format involves much more steps.
This article describes the entire request to response lifecycle with steps which are managed by spring.

When a user makes a request for a resource, for example :-
Request: http:// localhost:8080/users
Accept: application/json

This incoming request is handled by the DispatcherServlet , this is auto configured by Spring Boot. While creating a project through Spring Boot framework and when we mention the Spring Boot Starter Web as a dependency in pom.xml, Spring Boot’s auto configuration feature configures dispatcherServlet, a default error page and other dependent jar files.
When a Spring boot application is run, the log will have a message like this

[ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet’ to [/]

DispatcherServlet is the front controller and all incoming request goes through this single servlet
The process from a request to response are shown in the below flow chart

Capture1

The block in the green are the ones which is implemented by developers
In our request for /users resources below are the activities performed in each step

In Step 1, Dispatcher servlet will intercept the request for the resource /users

In Step 2, the servlet determines the handler for the request – A good link on this topic

In Step 3, Spring checks which controller method matches the incoming lookup path of “/users” request. Spring maintains a list of all mapping registry fetched from the @RequestMapping of controller class and iterates the list to look for the matching method in the controller class implemented by developer

In Step 4, after determining the right method it executes the controller method

Step 5 returns a ArrayList of users

The response type accepted by client can be either JSON/XML. Therefore, Step 6 does the job of marshalling the Java Object to the response type requested by client., Spring takes the ArrayList of users and uses the message converter method to marshal it to the type requested by client, in case if the message converted is not available then client will get a 406 error. In the case of users as the requested type is JSON, a JSON object for users is returned as a response.

Conclusion

Understanding the life cycle of a request and response process and other classes involved helps to understand the issues better and troubleshoot it easily. To check the process life cycle, in the eclipse Open Type DispatcherServlet class and add a breakpoint at doDispatch method. Thanks a lot for reading this article so far. If you have any question or suggestion then please drop a note and I’ll try to answer your question.

Leave a Reply