cxf.jaxrs:使用 JAXRSServerFactoryBean 时出现 IllegalArgumentException

发布于 2024-08-11 00:57:18 字数 1067 浏览 3 评论 0 原文

在单元测试中设置 JAXRS 测试服务时,我遇到了以下问题。 这是代码(取自 AbstractJUnit4SpringContextTests 派生的测试类):

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceBeans(applicationContext.getBean("searchXY"));
    sf.setAddress("http://localhost:9000/");
    sf.create();

    restClient = new RestTestClient();//custom class for client-side testing

    ....

    InputStream dummyRequestFileAsStream = getInputStreamForClasspathResource(
    DUMMY_REQUEST_FILE);
    LOGGER.info("Testing searchQuery ReST service access");
    int httpStatus = restClient.postXmlStream(
                    "http://localhost:9000/search/searchXY",
                    dummyRequestFileAsStream);

我将 XML 测试文件提供到服务中。 CXF 会不恰当地尝试将 xml 包装到 javax.xml.bind.JAXBElement 中,调用服务,并因 IllegalArgumentException(在反射 API 中)而失败,因为该服务当然不接受特定于 JAX-RS 的元素,而是接受我之前在 XSD 中定义的 SearchRequest 元素。

但是,当我将以下行插入到我的 spring 上下文中时,一切都很好:

<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />

还有其他人看到过这个吗?

I ran into the following issue when setting up a JAXRS test service in a unit test.
This is the code (taken from an AbstractJUnit4SpringContextTests-derived test class):

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceBeans(applicationContext.getBean("searchXY"));
    sf.setAddress("http://localhost:9000/");
    sf.create();

    restClient = new RestTestClient();//custom class for client-side testing

    ....

    InputStream dummyRequestFileAsStream = getInputStreamForClasspathResource(
    DUMMY_REQUEST_FILE);
    LOGGER.info("Testing searchQuery ReST service access");
    int httpStatus = restClient.postXmlStream(
                    "http://localhost:9000/search/searchXY",
                    dummyRequestFileAsStream);

I'm feeding an XML test file into the service. CXF would impertinently try to wrap the xml into a javax.xml.bind.JAXBElement, invoke the service, and fail with an IllegalArgumentException (in the reflection API) because the service of course does not accept a JAX-RS-specific element but rather the SearchRequest element that I defined in my XSD before.

However, when I insert the following line into my spring context, everything's fine:

<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />

Anyone else seen this?

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

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

发布评论

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

评论(2

小猫一只 2024-08-18 00:57:18

如果不提供更多详细信息,很难理解为什么会发生最初的问题。上面的导入始终是必需的,并且我从未尝试过在没有 Spring 上下文的情况下进行测试。异常跟踪是什么?也许如果没有导入,JAXRS 拦截器甚至不会涉及?

干杯,谢尔盖

It's difficult to see why the original issue is happening without more details being provided. The above import is always required and I've never tried testing without it being in the spring context. What is the exception trace ? Perhaps the JAXRS interceptors are not even involved without the import ?

cheers, Sergey

内心激荡 2024-08-18 00:57:18

事实证明我错了:问题实际上出在 XSD 上:我有一个类型为“SearchRequest”(原文如此,大写 S)的 XSD 元素“searchRequest”,另外还有另一个使用扩展类型的根元素,派生来自搜索请求。看来 cxf 的类型存在问题,该类型既用作根元素的类型又用作 XSD 继承的类型。创建附加类型 AbstractSearchRequest 并使所有类型都继承自该类型后,问题就消失了。

It turns out I was wrong: The problem was actually with the XSD: I had an XSD element "searchRequest" that is of type "SearchRequest" (sic, capital S), and additionally another root element that is using an extended type, derived from SearchRequest. It seems that cxf is having problems with a type that is used both as a root element's type and as a type for XSD inheritance. After creating an additional type AbstractSearchRequest and having all types inherit from that type the problem went away.

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