users@jersey.java.net

Re: [Jersey] JaxbContextResolver

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 21 May 2010 11:11:47 +0200

On May 21, 2010, at 2:21 AM, Tatu Saloranta wrote:

> On Thu, May 20, 2010 at 9:59 AM, Sven Waibel <sven.waibel_at_web.de>
> wrote:
>> Good point!
>> But how would it be, if you specify an package name and it scan's
>> the entire package for classes and set's them into the context?
>
> There are packages that do this, using various hacks (JVM does not
> provide real proper way to do it, so mostly they just go through
> assumed classpath etc).

Yes you can use Jersey's PackageNamesScanner in conjunction with
AnnotationScannerListener (which uses ASM) to detect classes annotated
with @XmlRootElement or @XmlType. Or use your own implementation of
ScannerListener combined with ASM if you have specific requirements.

For example:

PackageNamesScanner pns = new PackageNamesScanner(new String[]
{"com.foo", "com.bar"});
AnnotationScannerListener asl = new
AnnotationScannerListener(XmlRootElement.class, XmlType.class);
pns.scan(asl);
Set<Class<?>> jaxbClasses = asl.getAnnotatedClasses();


You can also specify the context path in JAXB and include an artifact
required by JAXB as specified here:

http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html#newInstance%28java.lang.String,%20java.lang.ClassLoader%29
   Every package listed on the contextPath must meet one or both of
the following conditions otherwise a JAXBException will be thrown:

it must contain ObjectFactory.class
it must contain jaxb.index
> So you can probably connect such code to do automatic registration. I
> don't think JAXB implementations do this, and JAXB API specifically
> has no method to request such scanning.
>

Right. JAXB really requires things to be statically defined to
determine the whole "XML model".

When specifying classes explicitly JAXB will also recognize any
classes that are directly/indirectly referenced statically from those
specified classes.



> You could try asking JAXB list for this, since this is not really a
> Jersey or JAX-RS specific?
>
> -+ Tatu +-
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>