java - Jersey/Dropwizard 中的 Spring RequestContextHolder 是什么
问题描述
我是 Dropwizard 的新手,我们需要HttpServletRequest
在 AspectJ 项目中获取对象(本机而不是 AOP)。AspectJ项目在不同框架的不同项目中用作框架(jar)
下面的代码是HttpServletRequest
为 spring boot 项目获取的。
Class<?> requestHolder = Class.forName("org.springframework.web.context.request.RequestContextHolder");
Method method = requestHolder.getMethod("currentRequestAttributes");
Object currentAttributes = method.invoke(requestHolder);
Class<?> servletAttributes = Class.forName("org.springframework.web.context.request.ServletRequestAttributes");
currentAttributes = servletAttributes.cast(currentAttributes);
method = currentAttributes.getClass().getMethod("getRequest");
Object httpRequest = method.invoke(currentAttributes);
if (httpRequest instanceof HttpServletRequest) {
return (HttpServletRequest) httpRequest;
}
Jersey/Dopwizard 怎么办?
解决方案
You could inject the HttpServletRequest Object in your code as below
@Inject
private Provider<HttpServletRequest> requestProvider;
You could provide a method in you WebService class to access this request object. In your aspects you could use reflection to invoke this method and access the HttpServletRequest
AspectJ Part: Create an annotation that can be applied over the rest service method that triggers the aspect advice before/around/after method execution based on how you want to write it. Within the advice we invoke the getHTTPServlet method call by fetching the target object from the joinPoint
推荐阅读
- javascript - 让redis缓存每天在特定时间过期
- javascript - 为什么我的函数总计在计算器中不起作用
- java - 如何在 SpringBoot 中解析占位符值
- c++ - 带有 TLS 1.3 的“nghttp2::asio_http2::client” - SSL_CTX_set_cipher_list 不在密码套件中添加密码套件
- javascript - 仅当存在于数组过滤器中时才使用变量
- wildfly - Elytron 中的散列密码(simple-digest-mapper)
- c++ - 向量元素如何递增?
- ruby-on-rails - 为什么 Spring 会导致 Ruby 的权限问题
- python - 如何根据日期显示不同的营业时间?
- angular - 如何“http.get”一个准 JSON (JSONC) 配置文件(不是纯 JSON)