cxf.jaxrs:使用 JAXRSServerFactoryBean 时出现 IllegalArgumentException
在单元测试中设置 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" />
还有其他人看到过这个吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不提供更多详细信息,很难理解为什么会发生最初的问题。上面的导入始终是必需的,并且我从未尝试过在没有 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
事实证明我错了:问题实际上出在 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.