覆盖泽西岛的提供商 [英] Overriding included provider in Jersey

查看:75
本文介绍了覆盖泽西岛的提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Jersey中创建一个自定义的ExceptionMapper来处理Jackson返回的JsonProcessingException。

I need to make a custom ExceptionMapper in Jersey to handle the JsonProcessingException returned by Jackson.

Jackson库已经以JsonMappingExceptionMapper.java和JsonParseExceptionMapper.java( link )。

The Jackson library already includes ExceptionMapper providers for this exception in the form of JsonMappingExceptionMapper.java and JsonParseExceptionMapper.java (link).

如果我在my.package中为此异常映射器添加新的提供程序,则会得到有关所选提供程序的不可预测的结果。有时它会在my.package中选择提供者,有时它会选择Jackson库中的提供者。我用来扫描软件包的代码如下。

If I add a new provider for this exception mapper in "my.package" I get unpredictable results regarding the selected provider. Sometimes it will select the provider in "my.package" and sometimes it will select the provider in the Jackson library. The code I'm using to scan the packages is below.

PackgesResourceConfig packagesResourceConfig = new PackgesResourceConfig("com.fasterxml.jackson.jaxrs", "my.package");

建议的解决方案

目前我通过手动过滤Jackson库中的提供程序来解决这个问题。但我真正想知道的是,是否有更可接受和支持的方式。

Currently I am getting around this by filtering out the provider in the Jackson library manually. But what I really want to know is whether there is a more acceptable and supported way of doing this.

首先我扩展PackagesResourceConfig。

First I extend PackagesResourceConfig.

public class FilteredPackgesResourceConfig extends PackagesResourceConfig {

    private Set<Class<?>> classesToFilter = new HashSet<Class<?>>();

    public FilteredPackgesResourceConfig(String... packages) {
        super(packages);
    }

    public FilteredPackgesResourceConfig(Map<String, Object> props) {
        super(props);
    }

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = super.getClasses();

        if (classes != null && classesToFilter != null) {
            classes.removeAll(classesToFilter);
        }

        return classes;
    }

    public Set<Class<?>> getClassesToFilter() {
        return classesToFilter;
    }

    public void setClassesToFilter(Set<Class<?>> classesToFilter) {
        this.classesToFilter = classesToFilter;
    }
}

我使用此类过滤掉特定的提供者我不想要。

This I use this class to filter out the specific providers I don't want.

FilteredPackgesResourceConfig packagesResourceConfig = new FilteredPackgesResourceConfig("com.fasterxml.jackson.jaxrs", "my.package");
classesToFilter.add(com.fasterxml.jackson.jaxrs.json.JsonMappingExceptionMapper.class);       
classesToFilter.add(com.fasterxml.jackson.jaxrs.json.JsonParseExceptionMapper.class);
packagesResourceConfig.setClassesToFilter(classesToFilter);

此解决方案为我提供了仅使用我指定的提供程序所需的结果。
有没有更正确的方法来实现相同的结果?

This solution gives me the desired result of only using the providers I specified. Is there a more correct way of achieving the same result?

推荐答案

我也遇到过这个问题,在我的case我解决了它而不是注册 com.fasterxml.jackson.jaxrs.json 包我只注册了我想要的类,在我的例子中是 com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider

I also came across this problem, in my case I solved it by instead of registering com.fasterxml.jackson.jaxrs.json package I only registered the class I wanted, which in my case was com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider.

有几种方法可以做到这一点,我是用web.xml做的所以:

There are several ways to do this, I did it using web.xml like so:

<servlet>
  <servlet-name>jersey-serlvet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>
         my.own.package
         my.other.package
      </param-value>
  </init-param>
  <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
      com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
    </param-value>
  </init-param>
    <init-param>
      <param-name>jersey.config.disableMoxyJson</param-name>
      <param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

注意:我使用Jersey 2.0,在1.x中属性名称和servlet类是不同的但是可以使用相同的配置。

Note: I'm using Jersey 2.0, in 1.x the property names and servlet class are diferent but the same config is possible.

这篇关于覆盖泽西岛的提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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