可以和Jackson一起使用@XmlHeader吗? [英] Is it possible to use @XmlHeader together with Jackson?

查看:444
本文介绍了可以和Jackson一起使用@XmlHeader吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Jackson(注释)将我的对象编组到JSON& XML并且它工作得非常好但是XML存在问题。我想添加一个DTD-File。我搜索了一下,找到了@ XmlHeader-Annotation(org.glassfish.jersey.message.XmlHeader)来添加标题:

I'm marshalling my objects with Jackson (annotations) to JSON & XML and it works really great but there's a problem with XML.I want to add a DTD-File.I searched a little bit and found the @XmlHeader-Annotation (org.glassfish.jersey.message.XmlHeader) to add a header :

@Path("resources/xml/hashtagstatistic")
@GET
@XmlHeader("<!DOCTYPE note SYSTEM \"test.dtd\">")
@Produces(MediaType.APPLICATION_XML)

public Database getStatisticAsXml(){
    return serviceController.getDatabase();
}

但它不起作用。我用jaxb做了同样的事情,并且在我的XML-Output中添加了标题。但是我想使用Jackson,因为它返回了我想要的结构(我不喜欢/想要适配器)。是否有可能解决此问题或是否有其他解决方案来处理标题的这个问题?

But it's not working. I tried the same with jaxb and there the header was added to my XML-Output.But I want to use Jackson because it returns easy my wanted structure (I don't like/want adapters). Is there a possibility to fix this or are there other solutions to handle this problem with the header?

推荐答案

是的,这是泽西特定的注释,所以杰克逊不会对此有任何了解。我看到了几个选项。您可以使用 WriterInterceptor ,然后自己编写标题。

Yeah it's a Jersey specific annotation, so Jackson won't know anything about it. I see a couple options. You could use a WriterInterceptor, and just write the header yourself.

@Provider
public class XmlHeaderWriterInterceptor implements WriterInterceptor {

    @Context
    private ResourceInfo info;

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
                    throws IOException, WebApplicationException {
        final OutputStream outputStream = context.getOutputStream();

        XmlHeader anno = info.getResourceMethod().getAnnotation(XmlHeader.class);
        if (anno != null) {
            String value = anno.value();
            writeToStream(outputStream, value);
        }
        context.proceed();
    }
}

或者你可以创建 MessageBodyWriter 。但是,不是从头开始实现自己的,而是可以扩展杰克逊的那个(假设这是你目前使用的)

Or you could create a MessageBodyWriter. But instead of implementing your own from scratch, you could just extend the one from Jackson (assuming this is what you're currently using)

@Provider
public class MyJackonXmlProvier extends JacksonJaxbXMLProvider {

    @Context
    private ResourceInfo info;

    @Override
    public void writeTo(Object value, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String,Object> httpHeaders, 
                        OutputStream entityStream) {

        // do same thing as example above
        super.writeTo(pass, all, arguments)
}

您使用的是哪一个,只需确保将其注册到应用程序。

Which ever one you use, just make sure to register it with the application.

这篇关于可以和Jackson一起使用@XmlHeader吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆