Custom MOXyJsonProvider in Jersey 2 not working?

I read Moxy's answer to ignore invalid fields in json , and this approach was consistent with what I'm trying to do, so I decided to give it a chance .. I created a function to disable the standard 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 him a chance with a simple GET request;

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

I believe this should work, but neither preWriteTo nor preReadFrom are ever called. Is there another step that I am missing? How can I run them?

+4
source share
1 answer

- , . :

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

All Articles