Java Spring应用,在HTTP请求方法之后

huangapple 未分类评论39阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年7月24日 22:39:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定