Apache CXF JAX-RS 服务缺少 XML 文档启动

发布于 2024-12-10 22:12:06 字数 2321 浏览 0 评论 0原文

我正在使用 Apache CXF 和 Spring 开发 Web 服务。我的接口和配置已配置好,以便我同时拥有 REST 和 SOAP 服务。 Apache CXF 没有启动 XML 文档:

对于 SOAP 服务,我实现了一个拦截器,它工作得很好。代码如下:

public class CustomHeaderInterceptor extends AbstractPhaseInterceptor<Message> {

public CustomHeaderInterceptor() {
    super(Phase.PRE_STREAM);
    addBefore(StaxOutInterceptor.class.getName());
}

@Override
public void handleMessage(Message message) throws Fault {
    message.put(Message.ENCODING, "UTF-8");
    message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, Boolean.TRUE);
}
}

使用以下配置添加拦截器:

<bean id="customHeader" class="com.minervanetworks.xtv.stb.utils.CustomHeaderInterceptor" />

<cxf:bus>
    <cxf:inInterceptors>
        <ref bean="logInbound" />
    </cxf:inInterceptors>
    <cxf:outInterceptors>
        <ref bean="customHeader" />
        <ref bean="logOutbound" />
    </cxf:outInterceptors>
    <cxf:inFaultInterceptors>
        <ref bean="logInbound" />
    </cxf:inFaultInterceptors>
    <cxf:outFaultInterceptors>
        <ref bean="logOutbound" />
    </cxf:outFaultInterceptors>
</cxf:bus>

不幸的是,这不适用于我的 JAX-RS 服务器。 “StaxOutInterceptor.FORCE_START_DOCUMENT”由 StaxOutInterceptor 处理,当我使用 JAX-RS 时它不在链中。它无法手动添加,因为它依赖于处于 *ending 阶段并在 JAXRSOutInterceptor 之后调用的 StaxOutEndingInterceptor。

我也尝试出于相同目的实施处理程序,但没有成功。

这是我的 JAXRS 服务器配置:

<jaxrs:server id="restServer" address="/rest">
    <jaxrs:providers>
        <ref bean="systemExceptionMapper" />
        <ref bean="jaxbProvider" />
    </jaxrs:providers>
    <jaxrs:serviceBeans>
        ...
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
        <entry key="json" value="application/json" />
        <entry key="xml" value="application/xml" />
        <entry key="feed" value="application/atom+xml" />
        <entry key="html" value="text/html" />
    </jaxrs:extensionMappings>
</jaxrs:server>

任何类型的帮助 - 想法、建议,无论什么都会被采纳!

I am developing web services using Apache CXF and Spring. My interfaces and configuration are configured so that I have both REST and SOAP services.
Apache CXF is not putting the XML document start: <?xml version="1.0" encoding="UTF-8"?>

For the SOAP service I implemented an interceptor and it works perfectly. Here's the code:

public class CustomHeaderInterceptor extends AbstractPhaseInterceptor<Message> {

public CustomHeaderInterceptor() {
    super(Phase.PRE_STREAM);
    addBefore(StaxOutInterceptor.class.getName());
}

@Override
public void handleMessage(Message message) throws Fault {
    message.put(Message.ENCODING, "UTF-8");
    message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, Boolean.TRUE);
}
}

The interceptor is added with the following configuration:

<bean id="customHeader" class="com.minervanetworks.xtv.stb.utils.CustomHeaderInterceptor" />

<cxf:bus>
    <cxf:inInterceptors>
        <ref bean="logInbound" />
    </cxf:inInterceptors>
    <cxf:outInterceptors>
        <ref bean="customHeader" />
        <ref bean="logOutbound" />
    </cxf:outInterceptors>
    <cxf:inFaultInterceptors>
        <ref bean="logInbound" />
    </cxf:inFaultInterceptors>
    <cxf:outFaultInterceptors>
        <ref bean="logOutbound" />
    </cxf:outFaultInterceptors>
</cxf:bus>

Unfortunately this does not work for my JAX-RS server. The "StaxOutInterceptor.FORCE_START_DOCUMENT" is processed by the StaxOutInterceptor and it is not in the chain when I'm using JAX-RS. It could not be added manually since it depends on StaxOutEndingInterceptor which is in the *ending phase and is called after JAXRSOutInterceptor.

I also tried implementing handler for the same purpose but without any success.

Here is my JAXRS server configuration:

<jaxrs:server id="restServer" address="/rest">
    <jaxrs:providers>
        <ref bean="systemExceptionMapper" />
        <ref bean="jaxbProvider" />
    </jaxrs:providers>
    <jaxrs:serviceBeans>
        ...
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
        <entry key="json" value="application/json" />
        <entry key="xml" value="application/xml" />
        <entry key="feed" value="application/atom+xml" />
        <entry key="html" value="text/html" />
    </jaxrs:extensionMappings>
</jaxrs:server>

Any kind of help - ideas, suggestions, whatever will be appriciated!

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

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

发布评论

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

评论(1

最后的乘客 2024-12-17 22:12:06

在投入大量时间解决这个问题之后,我找到了以下解决方案。

Apache CXF 在编组集合时不会启动 XML 文档,这就是我的情况。这是 JAXBElement 提供程序中的一种遗漏。我意识到当编组单个对象时一切都很好。

因此,对我来说显而易见的解决方案是将我的集合包装在集合对象中,如下所示:

公共类 CollectionWrapper{

私有列表集合;

...

}

重构我的方法以返回 CollectionWrapper,而不是 List 完成了这项工作。

After lots of time invested in this problem, I found the following solution.

Apache CXF is not putting the XML Document Start when it is marshaling collections, which was my case. It is kind of omission in JAXBElement provider. I realized that everything is just fine when marshaling single objects.

So the obvious solution for me was to wrap my collection in a collection object, like this:

public class CollectionWrapper{

private List collection;

...

}

Refactoring my methods to return CollectionWrapper, rather than List did the job.

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