I have a Java server application that uses CXF to provide SOAP and REST web services. Currently it uses the reference implementation of JAX-B for XML marshalling/unmarshalling, but I have configured it to replace Jettison with Jackson for JSON marshalling/unmarshalling. I use Spring for DI and application context configuration.
The REST web service configuration snippets looks as follows:
web.xml
<servlet>
<display-name>Myapp REST Services</display-name>
<servlet-name>MyappWebServices</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyappWebServices</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
applicationContext.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<jaxrs:server id="myappCoreSvcRest" address="/rest">
<jaxrs:serviceBeans>
<ref bean="fooService" />
<ref bean="barService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:server>
This configuration works and will return either XML or JSON depending on the HTTP Accept header. What I like about this configuration is that it is based in Spring and it is super easy to create and use an alternate JSON encoder. Details on configuring CXF can be found here.
My problem is that now I have a new (additional) REST web service to provide and I would like to use a different JAX-B XML binding for this new web service. I understand that MOXy can do this, but I am unable to figure out how to configure a CXF end point so that it will use MOXy for marshalling/unmarshalling (and furthermore how to tell Moxy about my custom XML mapping file). I also would like this new web service to return either XML or JSON depending on the Accept header. I also have read that MOXy 2.4+ can handle that too!
Ideally I could use MOXy for this new endpoint without affecting the other existing servlets.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Off hand I do not know the exact config for CXF, but below I have provided some links to using MOXy with Spring. Please feel free to contact me, and I can help you implement this:
When using MOXy with a JAX-RS implementation you can use a
ContextResolver
to bootstrap from MOXy's external mapping file:For a Complext Example
For More Information on Using MOXy with Spring
Yes, JSON binding is being added to EclipseLink 2.4. To leverage this in your application it should be a simple matter of creating a
MessageBodyReader
and aMessageBodyWriter
:You may also be able to create an extension of
JSONProvider
:For More Information