You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by hdave <da...@gmail.com> on 2012/01/24 15:24:56 UTC

How to configure CXF to use different JAXBContextFactory?

I'd like to use MOXy for XML marshalling and unmarshalling in CXF because I
need some of their extensions to JAXB.

There documentation says to create a jax.properties file in the same
package(s) as the domain objects and put the following inside it:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory


But I cannot find anything in the CXF documentation (or the JAXB 2.1 spec)
about this "jaxb.properties" file and so I conclude it will not be honored. 
In any event, I'd prefer to create and configure the ContextFactory via
Spring.  Currently, my CXF configuration looks like this:

	<jaxrs:server id="myAppSvcRest" address="/rest">
		<jaxrs:serviceBeans>
			<ref bean="fooService" />
			<ref bean="barService" />
		</jaxrs:serviceBeans>

		<jaxrs:providers>
			<ref bean="jsonProvider" />
		</jaxrs:providers>

		<jaxrs:features>
			<cxf:logging />
		</jaxrs:features>
	</jaxrs:server>

Any idea how I go about telling CXF to use my JAXBContextFactory?  

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5281773.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 02/10/12 16:48, rhalferty wrote:
> Is there a similar solution for JAX-WS rather then JAX-RS?
>
> I am also trying to control the JAXBContext. Our web service objects contain
> a myriad of classes and without being able to cache the contexts our server
> creates a new context for each interface. We have attempted<property
> name="singleJaxbContext" value="true"/>  but we are unsure if it is working
> because it is also documented under JAX-RS.
>
I think it is actually done by default for JAX-WS

Sergey

>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5715370.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: How to configure CXF to use different JAXBContextFactory?

Posted by rhalferty <ry...@cgifederal.com>.
Is there a similar solution for JAX-WS rather then JAX-RS?

I am also trying to control the JAXBContext. Our web service objects contain
a myriad of classes and without being able to cache the contexts our server
creates a new context for each interface. We have attempted <property
name="singleJaxbContext" value="true"/> but we are unsure if it is working
because it is also documented under JAX-RS.



--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5715370.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 30/01/12 22:19, hdave wrote:
> Sergey,
>
> Just wanted to let you know that the approach you suggested worked fine for
> using MOXy as my XML marshaller/unmarshaller.  CXF use my custom context
> resolver that I put into the<providers>  list.
>
Thanks, very good. Glen who is the fan of MOXy :-) will be delighted.

> I am now moving on to using MOXy's new JSON capability for
> marshalling/unmarshalling JSON within my REST API.  In this case, it looks
> like I need to create a new JSON provider class and put that into the
> <providers>  list as well.  Will report back when/if that is working too.
>
Please do

Cheers, Sergey

> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5442878.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: How to configure CXF to use different JAXBContextFactory?

Posted by redcoat <da...@enstratius.com>.
Hi,

Am interested in using MOXy too - did you have success adding the new JSON
provider?

cheers,

David


hdave wrote
> Sergey,
> 
> Just wanted to let you know that the approach you suggested worked fine
> for using MOXy as my XML marshaller/unmarshaller.  CXF use my custom
> context resolver that I put into the 
> <providers>
>  list.
> 
> I am now moving on to using MOXy's new JSON capability for
> marshalling/unmarshalling JSON within my REST API.  In this case, it looks
> like I need to create a new JSON provider class and put that into the 
> <providers>
>  list as well.  Will report back when/if that is working too.





--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5735624.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by hdave <da...@gmail.com>.
Sergey,

Just wanted to let you know that the approach you suggested worked fine for
using MOXy as my XML marshaller/unmarshaller.  CXF use my custom context
resolver that I put into the <providers> list.

I am now moving on to using MOXy's new JSON capability for
marshalling/unmarshalling JSON within my REST API.  In this case, it looks
like I need to create a new JSON provider class and put that into the
<providers> list as well.  Will report back when/if that is working too.

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5442878.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,
On 24/01/12 18:35, hdave wrote:
> Inline
>
>
> Sergey Beryozkin-5 wrote
>>
>> @Provider is the annotation introduced by JAX-RS to facilitate the
>> scanning of class resources for custom providers be loaded automatically.
>>
>> I've never implemented that in CXF because it's kind of non-practical
>> IMHO. It can not work well in complex environments and is too
>> coarse-grained (ex, not possible to register multiple instances
>> configured differently) and it can not be customized. Narrowing down the
>> class path to scan for the providers is usually more of an effort than
>> registering a provider from Spring or CXFNonSpringJAXRSServlet :-)
>>
>> But having said it's something CXF (JAX-RS) lacks even though I do not
>> see using this feature myself (at the moment at least :-))...
>>
>
> I agree with you completely, I much prefer explicit registering of my
> providers rather than class path scanning.  In this way, I can also control
> which providers are used within particular contexts.
>
>
> Sergey Beryozkin-5 wrote
>>
>> Custom (JAXB) providers may want to check custom JAXBContexts and
>> ContextResolver is something that can be used. They need to be
>> registered as providers and in CXF case this means they have to be
>> referenced from jaxrs:providers (alongside with other providers)
>>
>
> Just to be clear then, the<jaxrs:providers>  stanza holds both providers as
> well as well as custom ContextResolvers?
>

Yes

> I'm planning on using something like this as a custom ContextResolver:
>
> ----------------------------------
> import org.eclipse.persistence.jaxb.JAXBContextFactory;
>
> @Provider
> public class MoxyJaxbContextResolver implements ContextResolver<JAXBContext>
> {
>
>      public JAXBContext getContext(Class<?>  type) {
>          try {
>              JAXBContext jc = JAXBContextFactory.createContext(new Class[]
> {Animals.class}, null);
>
>              return jc
>          } catch (JAXBException e) {
>              e.printStackTrace();
>          }
>          return null;
>      }
>

Yes, that should do it
Let me know please if it works
Sergey

> ----------------------------------
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5324425.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: How to configure CXF to use different JAXBContextFactory?

Posted by hdave <da...@gmail.com>.
Inline


Sergey Beryozkin-5 wrote
> 
> @Provider is the annotation introduced by JAX-RS to facilitate the 
> scanning of class resources for custom providers be loaded automatically.
> 
> I've never implemented that in CXF because it's kind of non-practical 
> IMHO. It can not work well in complex environments and is too 
> coarse-grained (ex, not possible to register multiple instances 
> configured differently) and it can not be customized. Narrowing down the 
> class path to scan for the providers is usually more of an effort than 
> registering a provider from Spring or CXFNonSpringJAXRSServlet :-)
> 
> But having said it's something CXF (JAX-RS) lacks even though I do not 
> see using this feature myself (at the moment at least :-))...
> 

I agree with you completely, I much prefer explicit registering of my
providers rather than class path scanning.  In this way, I can also control
which providers are used within particular contexts.


Sergey Beryozkin-5 wrote
> 
> Custom (JAXB) providers may want to check custom JAXBContexts and 
> ContextResolver is something that can be used. They need to be 
> registered as providers and in CXF case this means they have to be 
> referenced from jaxrs:providers (alongside with other providers)
> 

Just to be clear then, the <jaxrs:providers> stanza holds both providers as
well as well as custom ContextResolvers?

I'm planning on using something like this as a custom ContextResolver:

----------------------------------
import org.eclipse.persistence.jaxb.JAXBContextFactory;

@Provider
public class MoxyJaxbContextResolver implements ContextResolver<JAXBContext>
{

    public JAXBContext getContext(Class<?> type) {
        try {
            JAXBContext jc = JAXBContextFactory.createContext(new Class[]
{Animals.class}, null);

            return jc
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

----------------------------------

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5324425.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Dave


On 24/01/12 17:06, hdave wrote:
> Thank you Sergey, I'll try that.
>
> I think part of my problem is that I am confused about the Spring bean
> configuration of the CXF server.   I don't really understand what
> <jaxrs:server>  is doing inside.  Is it setting up an instance of
> JAXRSServerFactoryBean?
>
> Also what is supposed to go into<jaxrs:providers>?  Anything with an
> "@Provider" annotation or anything that implements
> ContextResolver<JAXBContext>  ?

@Provider is the annotation introduced by JAX-RS to facilitate the 
scanning of class resources for custom providers be loaded automatically.

I've never implemented that in CXF because it's kind of non-practical 
IMHO. It can not work well in complex environments and is too 
coarse-grained (ex, not possible to register multiple instances 
configured differently) and it can not be customized. Narrowing down the 
class path to scan for the providers is usually more of an effort than 
registering a provider from Spring or CXFNonSpringJAXRSServlet :-)

But having said it's something CXF (JAX-RS) lacks even though I do not 
see using this feature myself (at the moment at least :-))...


>
> I ask because, looking into it, I am now surprised my Jackson JSON provider
> ever worked.  It does NOT implement ContextResolver<JAXBContext>.  Here is
> the class definition:

Custom (JAXB) providers may want to check custom JAXBContexts and 
ContextResolver is something that can be used. They need to be 
registered as providers and in CXF case this means they have to be 
referenced from jaxrs:providers (alongside with other providers)

CXF JAXBElementProvider and JSONProvider will check custom ContextResolver

>
> -----------------------------------------------------------------
> @Provider
> @Consumes({MediaType.APPLICATION_JSON, "text/json"})
> @Produces({MediaType.APPLICATION_JSON, "text/json"})
> public class JacksonJsonProvider
>      implements
>          MessageBodyReader,
>          MessageBodyWriter,
>          Versioned // since 1.6
>
> .....
> -----------------------------------------------------------------
>
> How did this work?  What is the requirements for being placed into the
> <jaxrs:providers>  configuration stanza?

Just placing it there tells the runtime it must be a provider :-)
But I've no idea if JacksonJSONProvider checks ContextResolver or not

Cheers, Sergey


>
> Thanks,
> Dave
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5318636.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: How to configure CXF to use different JAXBContextFactory?

Posted by hdave <da...@gmail.com>.
Thank you Sergey, I'll try that.

I think part of my problem is that I am confused about the Spring bean
configuration of the CXF server.   I don't really understand what
<jaxrs:server> is doing inside.  Is it setting up an instance of
JAXRSServerFactoryBean?

Also what is supposed to go into <jaxrs:providers>?  Anything with an
"@Provider" annotation or anything that implements
ContextResolver<JAXBContext> ?

I ask because, looking into it, I am now surprised my Jackson JSON provider
ever worked.  It does NOT implement ContextResolver<JAXBContext>.  Here is
the class definition:

-----------------------------------------------------------------
@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
@Produces({MediaType.APPLICATION_JSON, "text/json"})
public class JacksonJsonProvider
    implements
        MessageBodyReader,
        MessageBodyWriter,
        Versioned // since 1.6

.....
-----------------------------------------------------------------

How did this work?  What is the requirements for being placed into the
<jaxrs:providers> configuration stanza?

Thanks,
Dave


--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5318636.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to configure CXF to use different JAXBContextFactory?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On 24/01/12 14:24, hdave wrote:
> I'd like to use MOXy for XML marshalling and unmarshalling in CXF because I
> need some of their extensions to JAXB.
>
> There documentation says to create a jax.properties file in the same
> package(s) as the domain objects and put the following inside it:
>
> javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
>
>
> But I cannot find anything in the CXF documentation (or the JAXB 2.1 spec)
> about this "jaxb.properties" file and so I conclude it will not be honored.
> In any event, I'd prefer to create and configure the ContextFactory via
> Spring.  Currently, my CXF configuration looks like this:
>
> 	<jaxrs:server id="myAppSvcRest" address="/rest">
> 		<jaxrs:serviceBeans>
> 			<ref bean="fooService" />
> 			<ref bean="barService" />
> 		</jaxrs:serviceBeans>
>
> 		<jaxrs:providers>
> 			<ref bean="jsonProvider" />
> 		</jaxrs:providers>
>
> 		<jaxrs:features>
> 			<cxf:logging />
> 		</jaxrs:features>
> 	</jaxrs:server>
>
> Any idea how I go about telling CXF to use my JAXBContextFactory?

You probably need to register (within the jaxrs:providers)
a custom ContextResolver<JAXBContext> implementation which would load
org.eclipse.persistence.jaxb.JAXBContextFactory and will delegate to it 
on all the ContextResolver calls

Give it a try please
Cheers, Sergey

>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-configure-CXF-to-use-different-JAXBContextFactory-tp5281773p5281773.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com