使用 Spring 配置 CXF 以使用 MOXY 进行 XML 编组/解组
我有一个 Java 服务器应用程序,它使用 CXF 提供 SOAP 和 REST Web 服务。目前,它使用 JAX-B 的参考实现进行 XML 编组/解组,但我已将其配置为用 Jackson 替换 Jettison 进行 JSON 编组/解组。我使用 Spring 进行 DI 和应用程序上下文配置。
REST Web 服务配置片段如下所示:
web.xml
<servlet>
<display-name>Myapp REST Services</display-name>
<servlet-name>MyappWebServices</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyappWebServices</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
applicationContext.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<jaxrs:server id="myappCoreSvcRest" address="/rest">
<jaxrs:serviceBeans>
<ref bean="fooService" />
<ref bean="barService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:server>
此配置有效,并将根据 HTTP Accept 标头返回 XML 或 JSON。我喜欢这个配置的原因是它基于 Spring,并且创建和使用备用 JSON 编码器非常容易。有关配置 CXF 的详细信息,请参阅此处。
我的问题是,现在我要提供一个新的(附加的)REST Web 服务,并且我想为这个新的 Web 服务使用不同的 JAX-B XML 绑定。我知道 MOXy 可以做到这一点,但我无法弄清楚如何配置 CXF 端点,以便它将使用 MOXy 进行编组/解组(以及如何告诉 Moxy 我的自定义 XML 映射文件)。我还希望这个新的 Web 服务根据 Accept 标头返回 XML 或 JSON。我还读到 MOXy 2.4+ 也可以处理这个问题!
理想情况下,我可以将 MOXy 用于这个新端点,而不会影响其他现有的 servlet。
I have a Java server application that uses CXF to provide SOAP and REST web services. Currently it uses the reference implementation of JAX-B for XML marshalling/unmarshalling, but I have configured it to replace Jettison with Jackson for JSON marshalling/unmarshalling. I use Spring for DI and application context configuration.
The REST web service configuration snippets looks as follows:
web.xml
<servlet>
<display-name>Myapp REST Services</display-name>
<servlet-name>MyappWebServices</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyappWebServices</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
applicationContext.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<jaxrs:server id="myappCoreSvcRest" address="/rest">
<jaxrs:serviceBeans>
<ref bean="fooService" />
<ref bean="barService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:server>
This configuration works and will return either XML or JSON depending on the HTTP Accept header. What I like about this configuration is that it is based in Spring and it is super easy to create and use an alternate JSON encoder. Details on configuring CXF can be found here.
My problem is that now I have a new (additional) REST web service to provide and I would like to use a different JAX-B XML binding for this new web service. I understand that MOXy can do this, but I am unable to figure out how to configure a CXF end point so that it will use MOXy for marshalling/unmarshalling (and furthermore how to tell Moxy about my custom XML mapping file). I also would like this new web service to return either XML or JSON depending on the Accept header. I also have read that MOXy 2.4+ can handle that too!
Ideally I could use MOXy for this new endpoint without affecting the other existing servlets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 的成员(JSR-222)专家组。
我暂时不知道 CXF 的确切配置,但下面我提供了一些将 MOXy 与 Spring 结合使用的链接。请随时联系我,我可以帮助你实现这个:
将 MOXy 与 JAX-RS 实现结合使用时,您可以使用
ContextResolver
从 MOXy 的外部映射文件引导:对于复杂示例
有关使用 MOXy 与 Spring 的更多信息
是的,JSON 绑定已添加到 EclipseLink 2.4 中。要在您的应用程序中利用此功能,创建
MessageBodyReader
和MessageBodyWriter
应该是一个简单的事情:您还可以创建
JSONProvider
的扩展代码>:了解更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Off hand I do not know the exact config for CXF, but below I have provided some links to using MOXy with Spring. Please feel free to contact me, and I can help you implement this:
When using MOXy with a JAX-RS implementation you can use a
ContextResolver
to bootstrap from MOXy's external mapping file:For a Complext Example
For More Information on Using MOXy with Spring
Yes, JSON binding is being added to EclipseLink 2.4. To leverage this in your application it should be a simple matter of creating a
MessageBodyReader
and aMessageBodyWriter
:You may also be able to create an extension of
JSONProvider
:For More Information