java - 在 Dropwizard 中启用 Jersey RequestDispatcher 需要哪些 jar

标签 java rest jersey dropwizard requestdispatcher

我正在处理 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 的导入。 :

enter image description here

IntelliJ IDEA 似乎可以识别类路径上的这些:

enter image description here

但是需要的是对 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

获取 URI 信息
@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/

相关文章:

error-handling - 来自 Jersey 的Dropwizard错误消息

java - 围绕 Java 操作应用超时控制

java - 如何获取本地化的日期模式字符串?

java - SSLEngine:在成功握手后展开时无效的 TLS 填充数据

rest - 如何使用 Soap UI 将响应作为附件获取

jquery - 泽西 REST + CORS + Jquery AJAX 调用

java - 将 json 对象中的日期发送到 Restful ws

java - 具有黑色不透明度的 JWindow

java - 如何在 Alfresco 中使用 Rest API 创建文档库

spring - jersey-spring3 实例化 Spring 管理的 bean (null!)