英文:
Java Spring Application, after HTTP request method
问题
我正在编写一个Java Spring微服务。我的控制器使用@PostMapping。
在HTTP请求完成后,我希望自动调用清理方法。这可能吗?
原因是我的应用程序可能会从不同的地方抛出异常,如果我有一个单独的方法在请求完成后被调用,那么我只需要在一个地方编写我的清理例程,而不用担心在服务中的新流程中遗漏清理代码。
英文:
I a writing a Java Spring Microservice. My controller uses @PostMapping
After a HTTP request to my controller completes I would like to automatically call a cleanup method. Is this possible?
Reason is my app can throw from different places and if I had one single method that gets called after the request completes then I can just write my cleanup routine in one place and not worry about leaving out cleanup code from new flows in the service.
答案1
得分: 0
你可以创建一个实现了 HandlerInterceptor
接口的类,然后重写 postHandle
或者 afterCompletion
方法。
英文:
You can have a class which implements HandlerInterceptor
and then over-ride postHandle
or afterCompletion
专注分享java语言的经验与见解,让所有开发者获益!
评论