在 Jersey 中注册自定义 ResourceMethodInvocationHandler [英] Registering a custom ResourceMethodInvocationHandler in Jersey

查看:26
本文介绍了在 Jersey 中注册自定义 ResourceMethodInvocationHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在它的 JSON 被解组后拦截一个资源调用.通过阅读一些论坛和帖子,我发现我可以通过实现 org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider 来做到这一点.这样做之后,我现在一直在尝试注册我的 CustomResourceMethodInvocationHandler 提供程序,以便 jersey/hk2 内部调用我重写的 public InvocationHandler create(Invocable invocable)方法.任何帮助将非常感激!

I am attempting to intercept a resource call after it's JSON has been unmarshalled. Reading through some forums and posts I discovered that I may be able to do so by implementing org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider. Having done so I am now stuck trying to get my CustomResourceMethodInvocationHandler provider registered so that the jersey/hk2 internals call my overridden public InvocationHandler create(Invocable invocable) method. Any help would be much appreciated!

推荐答案

我们来看看这个方法:

(使用 Jersey 2.10JSON 序列化测试)

(Tested with Jersey 2.10 and JSON serialization)

==============

==============

package com.example.handler;

import java.lang.reflect.InvocationHandler;

import org.glassfish.jersey.server.model.Invocable;
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider;

public class CustomResourceInvocationHandlerProvider implements
        ResourceMethodInvocationHandlerProvider {

    @Override
    public InvocationHandler create(Invocable resourceMethod) {
            return new MyIncovationHandler();
    }

}

<小时>

2) 实现自定义 InvocationHandler

package com.example.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyIncovationHandler implements InvocationHandler {

    @Override
    public Object invoke(Object obj, Method method, Object[] args)
            throws Throwable {
        // optionally add some logic here
        Object result = method.invoke(obj, args);
        return result;
    }
}

<小时>

3) 创建自定义 Binder 类并注册您的 CustomResourceInvocationHandlerProvider

package com.example.handler;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider;

public class CustomBinder extends AbstractBinder {

    @Override
    protected void configure() {
        // this is where the magic happens!
        bind(CustomResourceInvocationHandlerProvider.class).to(
                ResourceMethodInvocationHandlerProvider.class);
    }
}

<小时>

4) 可选:在 ResourceMethodInvocationHandlerFactory 中设置断点

只是为了了解org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactoryResourceMethodInvocationHandlerProvider的选择是如何工作的.


4) Optionally: Set breakpoint in ResourceMethodInvocationHandlerFactory

Just to understand how the selection of ResourceMethodInvocationHandlerProvider in org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory works.

==============

==============

如您所见,最重要的是将您的 CustomResourceInvocationHandlerProvider.class 绑定到 ResourceMethodInvocationHandlerProvider.class.完成此操作后,HK2 会知道您的 Provider 以及您的 Handler!

As you can see, the most important thing is to bind your CustomResourceInvocationHandlerProvider.class to the ResourceMethodInvocationHandlerProvider.class. After doing this, HK2 knows about your Provider and also about your Handler!

希望,我能帮上忙.

这篇关于在 Jersey 中注册自定义 ResourceMethodInvocationHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆