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

查看:217
本文介绍了在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!

推荐答案

让我们来看看这种方法:

Let's have a look at this approach:

(已通过 Jersey 2.10 JSON 序列化测试)

(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.ResourceMethodInvocationHandlerFactory 中选择 ResourceMethodInvocationHandlerProvider 的工作方式.


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会知道您的提供者以及您的处理者!

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天全站免登陆