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?
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With