7

Background

We are using Apache CXF 2.5.x , JSR 311 1.1.1 , and Spring 2.5.x

We currently have 2 endpoints, ABC and DEF, that use Jackson as the JSON provider. Our Spring file looks something like:

<bean id="jacksonMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="jsonProvider"class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" p:mapper-ref="jacksonMapper"/>

<jaxrs:server id="service1" address="/">
    <jaxrs:serviceBeans>
        <ref bean="resourceABC" />
        <ref bean="resourceDEF" />
    </jaxrs:serviceBeans>
    <jaxrs:providers >
         <ref bean="jsonProvider"/>
    </jaxrs:providers>
</jaxrs:server>

Using annotations on the Java classes, these endpoints are available via http://company.com/api/rest/ABC and http://company.com/api/rest/DEF

Challenge

We want to introduce a new endpoint, IJK, that uses Gson as a JSON provider. We would like the endpoint to map to http://company.com/api/rest/IJK.

Note: Indeed, it is trivially easy to map to http://company.com/api/rest/new/IJK by using a different endpoint, but we'd like to avoid that.

Approaches

We've tried defining a new server with the same address:

<jaxrs:server id="service2" address="/">
    <jaxrs:serviceBeans>
        <ref bean="resourceIJK" />
    </jaxrs:serviceBeans>
    <jaxrs:providers >
         <ref bean="gsonProvider"/>
    </jaxrs:providers>
</jaxrs:server>

but that doesn't work. We've tried using multiple providers in the same server element, but no dice.

This link discusses using serverFactories elements. Such as:

<beans>
  <jaxrs:server id="customerService" address="/service1">
    <jaxrs:serviceFactories>
      <ref bean="sfactory1" />
      <ref bean="sfactory2" />
    </jaxrs:serviceFactories>
  </jaxrs:server>

  <bean id="sfactory1" class="org.apache.cxf.jaxrs.spring.SpringResourceFactory">
     <property name="beanId" value="customerBean1"/>
  </bean>
  <bean id="sfactory2" class="org.apache.cxf.jaxrs.spring.SpringResourceFactory">
     <property name="beanId" value="customerBean2"/>
  </bean>

  <bean id="customerBean1" class="demo.jaxrs.server.CustomerRootResource1" scope="prototype"/>
  <bean id="customerBean2" class="demo.jaxrs.server.CustomerRootResource2"  scope="prototype"/>
</beans>

This looks promising, but how to set the JSON provider for the services, customerBean1 and customerBean2 in Spring?

Question

Can someone clarify the last approach above, with serviceFactories? Can we achieve the goal of introducing resourceIJK (at the same endpoint root) with Gson instead of Jackson?

If it can be done only with Apache CXF 2.7, that is OK and useful info.

4

0 回答 0