6

我知道在使用 Marshaller 时如何打开格式化的可能性。但我正在使用 Apache CXF (JAX-RS) 并返回类似return Response.ok(entity).build();.

我还没有找到如何格式化输出的任何选项。我该怎么做?

4

2 回答 2

11

首先,获取 XML 格式输出的方法是在编组器上设置正确的属性(通常是使用 CXF 时的 JAXB,因为 JAXB 做得不错)。也就是说,您将在某处执行此操作:

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

问题是您不一定要格式化所有输出。它增加了相当多的开销。幸运的是,您已经生成了一个 explicit Response,所以我们可以使用它的更多功能:

Marshaller marshaller = JAXBContext.newInstance(entity.getClass()).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(entity, sw);

return Response.ok(sw.toString(), MediaType.APPLICATION_XML_TYPE).build();

这个 JIRA 问题中提到了另一种方法(本身已关闭,但这对您来说不是什么大问题):

解决方法是注册一个自定义输出处理程序,它可以检查用于请求可选缩进的任何自定义查询:

http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java

JAXBElementProvider 和 JSONProvider 由 JAXB Marshaller 驱动,因此默认情况下它们会检查当前消息的 Marshaller.JAXB_FORMATTED_OUTPUT 属性。

这导致如下代码:

public class FormattedJAXBInterceptor extends AbstractPhaseInterceptor<Message> {
    public FormattedJAXBInterceptor() {
        super(Phase.PRE_STREAM);
    }

    public void handleMessage(Message message) {
        message.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    }

    public void handleFault(Message message) {
        message.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    }
}

CXF 网站讨论了拦截器的注册

于 2012-04-14T17:53:20.730 回答
1

你可以实现一个MessageBodyWriter. 这是一种 JAX-RS 机制,允许您覆盖对象编组为 XML 的方式。

package org.example;

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;

@Provider
@Produces(MediaType.APPLICATION_XML)
public class FormattingWriter implements MessageBodyWriter<Object>{

    @Context
    protected Providers providers;

    public boolean isWriteable(Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public void writeTo(Object object, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException,
        WebApplicationException {
        try {
            ContextResolver<JAXBContext> resolver 
                = providers.getContextResolver(JAXBContext.class, mediaType);
            JAXBContext jaxbContext;
            if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
                jaxbContext = JAXBContext.newInstance(type);
            }
            Marshaller m = jaxbContext.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(object, entityStream);
        } catch(JAXBException jaxbException) {
            throw new WebApplicationException(jaxbException);
        }
    }

    public long getSize(Object t, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

}

了解更多信息

Below is a link to a complete example where a MessageBodyWriter is used as part of a JAX-RS service.

于 2012-04-14T21:06:46.713 回答