CXF中的java格式XML输出?
我知道在使用Marshaller时如何打开格式。但是我正在使用ApacheCXF(JAX-RS)并返回类似return Response.ok(entity).build();
的响应
我还没有找到任何设置输出格式的选项。我怎么做
你可以在下面搜索框中键入要查询的问题!
我知道在使用Marshaller时如何打开格式。但是我正在使用ApacheCXF(JAX-RS)并返回类似return Response.ok(entity).build();
的响应
我还没有找到任何设置输出格式的选项。我怎么做
# 1 楼答案
您可以实现一个
MessageBodyWriter
。这是一种JAX-RS机制,允许您重写对象如何编组为XML了解更多信息
下面是一个完整示例的链接,其中
MessageBodyWriter
被用作JAX-RS服务的一部分# 2 楼答案
首先,获取XML格式化输出的方法是在封送器上设置正确的属性(在使用CXF时通常是JAXB,这是可以的,因为JAXB做了一项可信的工作)。也就是说,在某个地方你会有这样的东西:
问题是,您不一定要格式化all输出;它增加了相当多的开销。幸运的是,您已经在生成一个显式的
Response
,因此我们可以使用它的更多功能:另一种方法在this JIRA issue中提到(它本身是封闭的,但这对您来说不是什么问题):
这导致了如下代码:
CXF站点discusses registration of interceptors