users@jersey.java.net

[Jersey] How to indent XML?

From: Frederic Bergeron <FBergeron_at_rocketmail.com>
Date: Thu, 20 Jan 2011 23:25:11 -0800 (PST)

Hi,

I would like to enable indentation when outputting XML with Jersey for debugging. I've read how to do that here:

http://markmail.org/message/lwazweptwslmshcw

Here is my code that performs the same thing (or so I think):

// Import declarations...

public final class JAXBContextAdapter extends JAXBContext {
    
    static {
        System.out.println( "Loading JAXBContextAdapter" );
    }

    public JAXBContextAdapter( JAXBContext context ) {
        this.context = context;
    }

    public Validator createValidator() throws JAXBException {
        return( context.createValidator() );
    }

    public Unmarshaller createUnmarshaller() throws JAXBException {
        return( context.createUnmarshaller() );
    }

    public Marshaller createMarshaller() throws JAXBException {
        Marshaller m = context.createMarshaller();
        m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
        return( m );
    }

    private final JAXBContext context;

}


// Import declarations

@Provider
public class MyJAXBContextResolver implements ContextResolver<JAXBContext> {

    static {
        System.out.println( "Loading MyJAXBContextResolver" );
    }

    public MyJAXBContextResolver() throws Exception {
        this.types = new HashSet( Arrays.asList( cTypes ) );
        this.context = new JAXBContextAdapter( JAXBContext.newInstance( cTypes ) );
    }

    public JAXBContext getContext( Class<?> objectType ) {
        return( ( types.contains( objectType ) ) ? context : null );
    }

    private final JAXBContext context;
    private final Set<Class> types;

    private Class[] cTypes = { RegistryStore.class, Registry.class, ItemIdentifier.class, AccessPolicyType.class, ContentCollectionType.class,
        IdentifierType.class, LangStringType.class, MetadataCollectionType.class, PropertyType.class, ProtocolImplementationDescriptionType.class,
        ProtocolType.class, ResponsibleType.class, TargetType.class, ToAccessPolicyRelationshipType.class, ToCollectionRelationshipType.class,
        ToContentCollectionRelationshipType.class, ToMetadataCollectionRelationshipType.class, ToTargetRelationshipType.class, VocabTermType.class
    };

}

As you can see, I have inserted 2 static statements to output a trace to know whether my classes are loaded or not. I don't see these traces on the log file so I assume that my classes are not loaded.

What am I doing wrong?

I also wonder if there is a better way to do that. The proposed solution is 2 years old. Maybe there is a newer and better way available now.

Frederic Bergeron, Licef