java - 在spring拦截器中获取请求映射对象,获取实际的url字符串模式
问题描述
可能很难解释为什么,但是我有这种情况,我需要获取当前请求的 url 的请求 url 映射字符串。
Like if I have a GET URL as "/Test/x/{number}"
I want to get "/Test/x/{number}" not "/Test/x/1"
我可以在拦截器中获取实际声明的 url 字符串吗?
如果这是可能的,我怎么能做到这一点
解决方案
您可以实现一个HanderInterceptor
来拦截、预先或发布、请求和自省被调用的方法。
public class LoggingMethodInterceptor implements HandlerInterceptor {
Logger log = LoggerFactory.getLogger(LoggingMethodInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod method = (HandlerMethod) handler;
GetMapping mapping = method.getMethodAnnotation(GetMapping.class);
log.info("URL is {}", Arrays.toString(mapping.value()));
return true;
}
}
这将输出,URL is [/hello/{placeholder}]
完整的例子可以在这里找到,https://github.com/Flaw101/spring-method-interceptor
您可以添加更多逻辑来内省某些方法、某些类型的请求等。
推荐阅读
- python - 将 datatime 列转换为 18 位 LDAP 时间戳
- apache-kafka - kafka 流中的 x-subscription-response-topic 的结束偏移值是 x-subscription-registration-topic 的两倍
- javascript - 如何从对象数组中读取特定值并将所有值添加到一个变量中?
- sql - 多个最大值
- json - 如何在 JSON 中创建新行?
- android - “SELECT DISTINCT _id”中“SELECT”附近的语法错误
- android-studio - 更新 gradle 插件后,Android Studio 不会在 kotlin 类中显示错误
- c++ - 将对象数组存储在自己的类 C++ 中
- swift - 如何以格式化样式快速获取系统时间?
- ionic-framework - 如果页面(演练)已被查看一次,如何更改应用程序路由路径