泽西岛上的ContextResolver和Provider是什么? [英] What is a ContextResolver and Provider in Jersey?

查看:325
本文介绍了泽西岛上的ContextResolver和Provider是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

泽西岛上的ContextResolver是什么,Provider是什么?两者有什么区别?我正在将Genson与Jersey一起使用.当Jersey在类路径上找到Genson JAR时,Genson将自动注册. Genson JAR的WEB-INF/services目录包含一个名为"org.glassfish.jersey.internal.spi.AutoDiscoverable"的文件.

What is a ContextResolver in Jersey and what is a Provider? What is the difference between the two? I am using Genson with Jersey. Genson is auto-registered when Jersey finds the Genson JAR on the classpath. The Genson JAR's WEB-INF/services directory contains a file called "org.glassfish.jersey.internal.spi.AutoDiscoverable".

遵循该AutoDiscoverable路径,默认情况下,Genson/Jersey自动注册以下类:

Following that AutoDiscoverable path, by default Genson/Jersey auto-registers a the following class:

@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json", "application/*+json"})
@Produces({MediaType.APPLICATION_JSON, "text/json", "application/*+json"})
public class GensonJsonConverter implements MessageBodyReader<Object>, MessageBodyWriter<Object> {

     private final ContextResolver<GensonJaxRSFeature> _gensonResolver;

更多的困惑在这里发挥作用:查看Genson文档,它建议创建一个自定义提供程序,如下所示:

Here's where more confusion comes into play: looking at the Genson documentation it recommends to create a custom provider like so:

    @Provider
    public class GensonProvider implements ContextResolver<Genson> {
    private final Genson genson = new GensonBuilder().setSkipNull(true).create();
    }

但是,该提供程序实现的是ContextResolver而不是像内部Genson那样实现的MessageBodyReader/Writer.有什么不同?另外,该提供程序与默认自动注册的提供程序不会做相同的事情!特别是,它会忽略 JAXB标签,例如@XmlTransient!深入研究GensonJaxRSFeature的Genson源代码,我看到Genson对象是这样创建的:

However that provider implements a ContextResolver not a MessageBodyReader/Writer like the internal Genson one does. What's the difference? Also, that provider does not do the same thing as the default auto-registered one does! In particular, it ignores JAXB tags like @XmlTransient! Digging into the Genson source code for GensonJaxRSFeature, I see that the Genson object is created like so:

private static final Genson _defaultGenson = new GensonBuilder()
      .withBundle(new JAXBBundle())
      .useConstructorWithArguments(true)
      .create();

从那以及从Genson文档中,我可以看到"JAXBBundle"可能是引起Genson注意JAXB注释的原因.

From that and from the Genson documentation I can see that the "JAXBBundle" is probably what causes Genson to pay attention to the JAXB annotations.

主要问题:

我想使用在Jersey上自动注册的默认Genson JSON提供程序,但是我想在上面设置一些自定义属性.正如我所说,当我注册自定义提供程序时,它不使用默认的Genson一个!

I want to use the default Genson JSON provider that is auto-registered with Jersey, but I want to set a few custom properties on it. As I said, when I register my custom provider, it doesn't use the default Genson one!

更新:

这就是我现在正在做的并且有效.但是,@ eugen下面的解决方案是Genson推荐的解决方案.

This is what I'm doing right now and it works. However, the solution below by @eugen is the Genson recommended solution.

@Provider
public class GensonProvider implements ContextResolver<GensonJaxRSFeature> {
    private final GensonJaxRSFeature _gensonResolver = new GensonJaxRSFeature();

    private static final Genson _defaultGenson = new GensonBuilder()
              .withBundle(new JAXBBundle())
              .useConstructorWithArguments(true)
              .setSkipNull(true)
              .create();

   @Override
   public GensonJaxRSFeature getContext(Class<?> type) {
        return _gensonResolver.use(_defaultGenson);
   }
}

推荐答案

就像我们世界上一样,对于相同的问题,有多种解决方案. 泽西岛似乎鼓励使用ResourceConfig而不是定义自定义提供程序.因此,这就是您使用资源配置来实现它的方法(来自jersey docs 此处和Genson文档此处))

Like always in our world for the same problem there are multiple solutions. Jersey seems to encourage the use ResourceConfig instead of defining custom providers. So this is how you can achieve it using a resource config (from jersey docs here and Genson documentation here).

public class MyApplication extends ResourceConfig {
    public MyApplication() {
      Genson genson = new GensonBuilder()
              .withBundle(new JAXBBundle())
              .useConstructorWithArguments(true)
              .setSkipNull(true)
              .create();

      register(new GensonJaxRSFeature().use(genson));
    }
}

但是,当然,您与提供者进行合作的方式也很好.

But of course the way you do it with a provider is fine too.

这篇关于泽西岛上的ContextResolver和Provider是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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