【问题标题】:Custom MOXyJsonProvider in Jersey 2 not working?Jersey 2 中的自定义 MOXyJsonProvider 不起作用?
【发布时间】:2015-05-18 19:40:23
【问题描述】:

我正在阅读Moxy ignore invalid fields in json 的答案,并且该方法与我正在尝试做的事情相匹配,所以我决定试一试。我创建了一个功能来禁用默认的 ConfigurableMoxyJsonProvider;

@Provider
public class JsonFeature implements Feature {
    @Override
    public boolean configure(final FeatureContext context) {
        final String disableMoxy = CommonProperties.MOXY_JSON_FEATURE_DISABLE +
                '.' +
                context.getConfiguration().getRuntimeType().name().toLowerCase();
        context.property(disableMoxy, true);
        return true;
    }
}

我创建了一个非常简单的自定义提供程序;

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JsonProvider extends MOXyJsonProvider {
    @Override
    protected void preWriteTo(Object object, Class<?> type, Type genericType,
                              Annotation[] annotations, MediaType mediaType,
                              MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller)
            throws JAXBException {
        System.out.println("test");
    }

    @Override
    protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations,
                               MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
                               Unmarshaller unmarshaller)
            throws JAXBException {
        System.out.println("test");
    }
}

我都注册了;

register(JsonFeature.class);
register(JsonProvider.class);

我尝试了一个简单的 GET 请求;

@GET
@Path("test")
public String getTest() {
    return new TestObject();
}

我相信这应该可行,但是 preWriteTo 和 preReadFrom 都不会被调用。我还缺少另一个步骤吗?我如何让这些开火?

【问题讨论】:

    标签: java json jersey jersey-2.0 moxy


    【解决方案1】:

    想通了——对于任何偶然发现它的人。关闭默认设置的正确方法是;

    @Provider
    public class JsonFeature implements Feature {
        @Override
        public boolean configure(final FeatureContext context) {
            context.property(CommonProperties.MOXY_JSON_FEATURE_DISABLE_SERVER, true);
            return true;
        }
    }
    

    然后像这样扩展 ConfigurableMoxyJsonProvider;

    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public class JsonProvider extends ConfigurableMoxyJsonProvider {
        @Override
        protected void preWriteTo(Object object, Class<?> type, Type genericType,
                                  Annotation[] annotations, MediaType mediaType,
                                  MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller)
                throws JAXBException {
            System.out.println("test");
        }
    
        @Override
        protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations,
                                   MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
                                   Unmarshaller unmarshaller)
                throws JAXBException {
            System.out.println("test");
        }
    }
    

    【讨论】:

    • 谢谢 :) 我也因为这个而碰壁。顺便提一句。您不需要创建 JSONFeature 类。您可以在 ResourceConfig resourceConfig.property(CommonProperties.MOXY_JSON_FEATURE_DISABLE_SERVER, true) 中设置属性
    • 感谢 2020 年 :)...moxy 的 javadocs 需要更新!
    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2016-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多