“org.apache.cxf.jaxrs.bus.providers” not working

戏子无情 提交于 2019-12-10 22:07:13

问题


I'm creating 2 REST containers using . I want to keep some common things like the JSON providers, validation interceptor, exception handling using a cxf bus. below is my application context.

<cxf:bus>
    <cxf:properties>
       <entry key="org.apache.cxf.jaxrs.provider" key-ref="busProviders"/>
    </cxf:properties>
</cxf:bus>

  <util:list id="busProviders">
    <ref bean="requestInterceptor"/>
    <ref bean="jsonProvider"/>
    <ref bean="exceptionHandler"/>
  </util:list>

<bean id="requestInterceptor" class="com.sample.interceptor.ServiceValidationInterceptor"/>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<bean id="exceptionHandler" class="com.sample.exception.ExceptionHandler" />

<!-- ======================== REST Container for Service1 ======================================= -->
 <jaxrs:server id="restContainer" address="/service1">
        <jaxrs:serviceBeans>
            <ref bean="endecaService"/>
        </jaxrs:serviceBeans>
        <jaxrs:inInterceptors>
            <ref bean="logInbound"/>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor">
            <property name="callbackParam" value="callback"/>
            </bean>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <ref bean="logOutbound"/>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor">
            <property name="mediaType" value="application/json"/>
            </bean>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor">
            <property name="paddingEnd" value=")"/>
            </bean>
        </jaxrs:outInterceptors>   
  </jaxrs:server>  

  <!-- ======================== REST Container for Service2========================================== -->
<jaxrs:server id="restContainerForHelpcenter" address="/service2">
    <jaxrs:serviceBeans>
        <ref bean="helpCenter"/>
    </jaxrs:serviceBeans>
        <jaxrs:inInterceptors>
            <ref bean="logInbound"/>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor">
            <property name="callbackParam" value="callback"/>
            </bean>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <ref bean="logOutbound"/>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor">
            <property name="mediaType" value="application/json"/>
            </bean>
            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor">
            <property name="paddingEnd" value=")"/>
            </bean>
        </jaxrs:outInterceptors> 
  </jaxrs:server>

If I combine the above 2 into a single with 2 service beans, then it works fine, but not in this case. Has anybody used the cxf:bus feature for 2 different REST containers, please let me know.


回答1:


Notice s in the end of the property name org.apache.cxf.jaxrs.bus.providers




回答2:


The long story

I had a similar problem:

I want to use autodiscovery for the JAX-RS service beans, but if I use a <jaxrs:providers> tag inside the <jaxrs:server> tag but no <jaxrs:serviceBeans> tag, autodiscovery is simply not used.

This means I have to use autodiscovery for the JAX-RS providers as well, which wasn’t difficult for the two that are below our base package (for the Spring component scan) since they were already annotated @Provider. However, this doesn’t work as easily for Jackson (and there are good reasons to not include Jackson in the Spring component scan, as it will pick up too much as the package contains several annotated classes).

So I had to hang up the Jackson provider on the CXF bus. I found the docs, which weren’t helpful, and your post, which gave me part of the answer. I did need several iterations of cursing, learning about xsi:schemaLocation and all that, but in the end, I did it:

tl;dr

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
        <context:component-scan base-package="com.example.basepackage"/>
<!-- … -->
        <import resource="classpath:META-INF/cxf/cxf.xml"/>
        <jaxrs:server address="/"/>
        <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        <cxf:bus>
                <cxf:properties>
                        <entry key="org.apache.cxf.jaxrs.providers" value-ref="busProviders"/>
                </cxf:properties>
        </cxf:bus>
        <util:list id="busProviders">
                <ref bean="jsonProvider"/>
        </util:list>
</beans>

… ?

The snippet I’ve elided is comprised of just a few manual bean definitions like these:

        <bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
                <property name="linkAnyMediaTypeToXmlSchema" value="true"/>
        </bean>

So the snippet I posted might be enough to get one started.



来源:https://stackoverflow.com/questions/20700840/org-apache-cxf-jaxrs-bus-providers-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!