Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom MOXyJsonProvider in Jersey 2 not working?

I was reading over the answer for Moxy ignore invalid fields in json and the approach matched something I'm trying to do, so I decided to give it a shot.. I created a feature to disable the default 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;
    }
}

And I created a really simple custom provider;

@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");
    }
}

I registered both;

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

And I gave it a shot with a simple GET request;

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

I believe this should work, but neither preWriteTo nor preReadFrom ever get invoked.. Is there another step I'm missing? How do I get these to fire?

like image 950
XeroxDucati Avatar asked Sep 28 '22 12:09

XeroxDucati


1 Answers

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");
    }
}
like image 81
XeroxDucati Avatar answered Oct 03 '22 01:10

XeroxDucati