将自定义请求marshallers提交到JAXRSClientFactory

时间:2011-12-28 22:34:57

标签: java spring cxf

我正在尝试使用JAXRSClientFactory调用我的RESTful服务 - 而且我一直在为请求/响应映射类型提供配置(我需要序列化List)

代码如下所示:

JAXRSClientFactory.create("http://localhost:8080/", MyCoolService.class, "/path/to/client/config.xml");

config.xml看起来像:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        ">

    <jaxrs:client id="testClient" createdFromAPI="true">
        <jaxrs:providers>
            <bean class="my.provider.Class"/>
        </jaxrs:providers>
    </jaxrs:client>

</beans>

现在在调试客户端代码时,我可以看到在org.apache.cxf.jaxrs.provider.ProviderFactory中有调用

    MessageBodyWriter<T> mw = chooseMessageWriter(messageWriters, 
                                                  bodyType,
                                                  parameterType,
                                                  parameterAnnotations,
                                                  mediaType,
                                                  m);

然而 messageWriters 不包含我的提供者。我的代码有什么问题以及如何正确提供MessageBodyWriter?提前谢谢!

1 个答案:

答案 0 :(得分:5)

基本上问题与 createdFromAPI =“true”相关

所以我摆脱了XML文件并使用了JAXRSClientFactory的专用版本,它接受消息体提供者列表作为方法参数

如果需要提供基本身份验证 - 那么

    ClientConfiguration config = WebClient.getConfig(proxy);
    HTTPConduit conduit = (HTTPConduit) config.getConduit();
    AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
    authorizationPolicy.setUserName(USERNAME);                     
    authorizationPolicy.setPassword(PASSWORD);                   
    conduit.setAuthorization(authorizationPolicy);