Jersey: Entity Data Filtering and inherited fields

859 Views Asked by At

The chapter on Entity Data Filtering in the Jersey manual shows code that looks like this at example 17.17

@Inject
private Provider<ObjectProvider<ObjectGraph>> provider;

@Override
protected void preWriteTo(final Object object, final Class<?> type, final Type genericType, final Annotation[] annotations,
                          final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
                          final Marshaller marshaller) throws JAXBException {
    super.preWriteTo(object, type, genericType, annotations, mediaType, httpHeaders, marshaller);

    // Entity Filtering.
    if (marshaller.getProperty(MarshallerProperties.OBJECT_GRAPH) == null) {
        final Object objectGraph = provider.get().getFilteringObject(genericType, true, annotations);

        if (objectGraph != null) {
            marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, objectGraph);
        }
    }
}

If I run a near equivalent of this code and look at the contents of the objectGraph, I see that inherited fields are not present. That is, say the genericType above looks like this:

public class Child extends Parent {
    private String baz;
    private Foo foo;  // Some non-primitive type.
    // ...
}

and Parent looks like this:

public abstract class Parent {
   private int bar;
   // ...
}

If I call objectGraph.getFields() within preWriteTo() above, I'll see the baz field in Child but not bar from Parent. Likewise, if I loop through the contents of objectGraph.getSubgraphs() I'll see foo from Child, but no non-primitive types from Parent. Note that bar and other fields from Parent are correctly marshalled into the JSON output (using MOXy in this case), so we know JAXB is working.

Is the lack of access to inherited fields intentional? I would like to examine the fields in parent class(es) while within this logic, and perform manual conversion of a Jersey ObjectGraph to a MOXy equivalent, but either include or remove fields as specified by the caller. I cannot do this if I cannot access every field that is included in the output.

Note that I'm using Jersey 2.4 and MOXy 2.5.1.

2

There are 2 best solutions below

1
On

Check Java reflection api http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.html and http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html to get all information you need from objectGraph.getEntityClass()

0
On

I've made a pull request for a fix for this, currently waiting for my OCA to clear. Hopefully it's in the 2.7 release...

https://github.com/jersey/jersey/pull/68