我正在处理 article尝试在现有的最小 Dropwizard 项目之上添加一层基本审核功能。
本文还包含关联的 repository .
gradle.build在该存储库中,除了核心 DW 之外,似乎没有任何额外的依赖项:
dependencies {
compile(
'io.dropwizard:dropwizard-core:' + dropwizardVersion
)
testCompile(
'junit:junit:4.11',
'org.hamcrest:hamcrest-core:1.3',
'org.mockito:mockito-all:1.9.5',
'org.unitils:unitils-core:3.4.2'
)
}
我的构建是在 Maven 中,并且似乎包含等效的依赖项列表:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-bom</artifactId>
<version>${dropwizard.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
</dependencies>
当我尝试构建与 this 等效的内容时,无法识别 RequestDispatcher/HttpContext 的导入。 :
IntelliJ IDEA 似乎可以识别类路径上的这些:
但是需要的是对 com.sun.jersey.api.core.HttpContext
instead 的引用.
有人可以指出我这里可能缺少哪些额外的 jar 文件,或者可能是一些对 DW 之上审计功能的工作演示的引用。
提前谢谢您。
最佳答案
那篇文章使用的是较旧的 Dropwizard 版本,当时它仍在使用 Jersey 1.x。由于您使用的是 2.x 的较新版本,因此现在的方法是使用 ContainerRequestFilter
。您可以通过注入(inject)ResourceInfo
来获取资源信息。在 Jersey 1.x 中,使用了 RequestDispatcher
,因为当时有 ResourceInfo
这样的东西,所以这是一种获取资源类和资源方法的方法。
@Provider
public class AuditRequestFilter implements ContainerRequestFilter {
@Context
private ResourceInfo info;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Class<?> resourceClass = info.getResourceClass();
Method resourceMethod = info.getResourceMethod();
}
}
只需使用 Dropwizard 注册过滤器即可
env.jersey().register(AuditRequestFilter.class);
编辑
再次查看您链接到的代码,最好使用ContainerResponseFilter
,因为您想在审核之前检查状态代码。另外,要获取远程地址,您可以注入(inject)HttpServletRequest
。您还可以从 UriInfo
@Provider
public class AuditRequestFilter implements ContainerResponseFilter {
@Context
private HttpServletRequest request;
@Context
private ResourceInfo info;
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
int status = responseContext.getStatus();
String remoteAddr = request.getRemoteAddr();
UriInfo uriInfo = requestContext.getUriInfo();
Class<?> resourceClass = info.getResourceClass();
Method resourceMethod = info.getResourceMethod();
}
}
另请参阅:
关于java - 在 Dropwizard 中启用 Jersey RequestDispatcher 需要哪些 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638067/