'How to override the get and post behavior of the DispatcherServlet?

I am using the Spring MVC controllers for REST services. I want to override the GET and POST behavior of the DispatcherServlet. I want to execute some custom logic when some condition is met during GET and POST request, otherwise fallback on the DispatcherServlet implementation.

I implemented custom serlvet that is derived from Spring's DispatcherServlet. There are methods doGet() and doPost() on the HttpServlet interface but it seems that those methods have been declared as final either in DispatcherServlet or one of its base class. Hence I could not override doGet() and doPost().

From the documentation, I figured out two methods - doService() and doDispatch() that could be overridden. I am confused about the difference between those two methods.

  1. Which method - doService() or doDispatch() should I override?
  2. What is the difference between doService() and doDispatch()?
  3. How to figure out what http request - GET or POST it is?

Thanks.



Solution 1:[1]

The right way to do it is to create a Filter.

Note, however, that inspecting request payload is not a simple thing per se, because once you read the payload you cannot "unread" it without extra effort. See, for example, How can I log RESTful post data?

But overriding methods of DispatcherServlet instead of using filters won't make it simplier anyway, it just makes things less clear and violates SRP.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community