如何使用 Spring 为 JAX-RS 测试客户端配置 Json 提供程序?

发布于 2024-11-09 22:32:54 字数 1345 浏览 5 评论 0原文

我已经使用 Apache CXF 设置了 JAX-RS 服务,我想使用 Apache CXF JAX-RS 的客户端 API 对其进行测试。服务器已配置为使用 Jackson 作为 json 提供程序。现在我想为客户端做同样的事情:也就是说,让 Jacson 处理与 json 的转换。

不幸的是,我不确定如何“启用 spring”测试,因为我对 Spring 只有肤浅的了解。我在测试中以编程方式设置了一个提供者,但想知道如何通过 Spring 来做到这一点。所以

@BeforeClass
public static void setup() {
    ProviderFactory.getSharedInstance().registerUserProvider(new JacksonJsonProvider());
}

我不想只是在 springConfiguration.xml (或类似的东西:)中设置它。设置服务器端时,springConfiguration.xml 如下所示。

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>

<jaxrs:server id="restContainer" address="/">
    <jaxrs:serviceBeans>
        <ref bean="echoService"/>
    </jaxrs:serviceBeans>

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

    <jaxrs:extensionMappings>
        <entry key="json" value="application/json"/>
    </jaxrs:extensionMappings>
</jaxrs:server>

我尝试添加

<jaxrs:client id="restClient" >
    <jaxrs:providers>
        <ref bean="jsonProvider" />
    </jaxrs:providers>
</jaxrs:client>

到 springConfiguration.xml,但这没有做任何事情。毫不奇怪,因为我还没有设置我的 JUnit 测试来使用 Spring ...任何人都可以告诉我如何做到这一点,或者向我指出任何可以用来组装必要信息的好资源吗?

I have set up a JAX-RS service using Apache CXF, which I would like to test using Apache CXF JAX-RS' Client API. The server has been configured to use Jackson as the json provider. Now I would like to do the same for the client: that is, letting Jacson handle the conversion to/from json.

Unfortunately I am unsure of how to "spring enable" the tests, having only superficial knowledge of Spring. I got away with programmatically setting up a provider in the test, but would like to know how to do it through Spring. So instead of

@BeforeClass
public static void setup() {
    ProviderFactory.getSharedInstance().registerUserProvider(new JacksonJsonProvider());
}

I would want to just set it in springConfiguration.xml (or something like that :). When setting up the serverside, springConfiguration.xml looked like this.

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>

<jaxrs:server id="restContainer" address="/">
    <jaxrs:serviceBeans>
        <ref bean="echoService"/>
    </jaxrs:serviceBeans>

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

    <jaxrs:extensionMappings>
        <entry key="json" value="application/json"/>
    </jaxrs:extensionMappings>
</jaxrs:server>

I tried to just add

<jaxrs:client id="restClient" >
    <jaxrs:providers>
        <ref bean="jsonProvider" />
    </jaxrs:providers>
</jaxrs:client>

to springConfiguration.xml, but that did not do anything. No surprise, as I have not set up my JUnit tests to use Spring ... Could anyone tell me how to do this, or point me to any good resources with which I could assemble the necessary info?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-11-16 22:32:56

以下注释将在运行测试之前加载您的应用程序上下文。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext.xml"}) 
public class JaxRsTest {

  @Autowired
  MyBean myBean

  public void testMyBean() {
    //add some real tests here...
    assertNotNull(myBean)
  }

}

The following annotations will load your application context prior to tests being run.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext.xml"}) 
public class JaxRsTest {

  @Autowired
  MyBean myBean

  public void testMyBean() {
    //add some real tests here...
    assertNotNull(myBean)
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文