Custom MOXyJsonProvider in Jersey 2 not working?

一曲冷凌霜 提交于 2019-12-04 09:52:30

Figured it out -- for anyone that stumbles across it.. The correct way to turn off the default is;

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

Then extend ConfigurableMoxyJsonProvider like so;

@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");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!