jersey/dropwizard中的spring requestcontextholder的等价物是什么

ig9co6j1  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(460)

我是dropwizard的新手,我们需要把 HttpServletRequest 对象(本机而不是aop)。aspectj项目在不同框架的不同项目中用作框架(jar)
下面的代码是get 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;
}

泽西岛/dopwizard怎么办?

uxhixvfz

uxhixvfz1#

您可以在代码中注入httpservletrequest对象,如下所示

@Inject
private Provider<HttpServletRequest> requestProvider;

您可以在webservice类中提供一个方法来访问此请求对象。在您的方面中,您可以使用反射来调用此方法并访问httpservletrequest
aspectj部分:创建一个可以应用于rest服务方法的注解,该注解根据您想要的编写方式在方法执行之前/前后/之后触发方面建议。在通知中,我们通过从joinpoint获取目标对象来调用gethttpservlet方法调用

相关问题