Intercepting @RequestBody after serialization but before controller

The request body objects all implement a call to the Auditable interface, where you can set the timestamp username and lastUpdate. I would like to intercept the controller function calls after serialization but before it hits the controller, so can I use those values ​​in one place.

I looked at HandlerInterceptor.prehandle

, but this method is executed before serialization. Any suggestion on how I can do this?

+3


source to share


1 answer


You can use ControllerAdvice and it can help with these scenarios. It intercepts all controller requests and you can access the serialized domain object in the method. It may well be all the arguments that the requestMapping method takes. Hope this helps.



@ControllerAdvice
public class ControllerAdvisor {

    @ModelAttribute
    public void addAttributes(HttpServletRequest request, HttpServletResponse response,Model model, @RequestBody DomainObject domain) {
        domain.setUserName("test");
        // set other items that you want to do.
    }   

}

      

+2


source







All Articles