如何自定义Jersey JAXB序列化的XML输出 [英] How to customise the XML output of a Jersey JAXB serialisation

查看:106
本文介绍了如何自定义Jersey JAXB序列化的XML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些@javax.xml.bind.annotation.Xml ...注释类用于RESt Web服务。 Jersey是在一个Spring托管的Web容器中设置的,Web服务正在返回格式良好的xml。我们使用maven-enunciate-plugin来记录Web服务并为返回的xml文档创建xsd。我现在想将文档xsd文件用作返回的xml文件中的schemaLocation,以便xml验证不会抱怨缺少定义。如何为此配置XML序列化?

I have some @javax.xml.bind.annotation.Xml... annotated classes here intended for a RESt web service. Jersey is setup in a spring managed web container and the web service is returning a well formatted xml. We use the maven-enunciate-plugin to document the web service and create the xsd to the returned xml documents. I now would like to use the documentation xsd file as a schemaLocation within the returned xml file so that the xml validation won't complain about missing definions. How can I get the XML serialisation configured for this?

推荐答案

如果我没记错的话,我必须做一些事情来将命名空间标识符正确写入我生成的XML中。

If I remember correctly, I had to do a few of things to get namespace identifiers properly written into my generated XML.

1)创建一个配置并返回自定义编组器的JaxbFactory(以及unmarshaller,BTW)。我省略了下面的getter /和解组设置......

1) Created a JaxbFactory that configs and returns a custom marshaller (and unmarshaller, too, BTW). I'm omitting the getters/and unmarshalling setup below...

//constructor
public JaxbFactory() throws Exception {
    context = JAXBContext.newInstance(ResourceDto.class);

    // Setup the marshaller
    marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XmlMetadataConstants.XML_SCHEMA_LOCATION);  // this schema location is used in generating the schema-location property in the xml
}

2)泽西岛的工厂级别不可见。为了使它可见,我创建了一个MarshallerProvider。看起来像这样:

2) That factory class isn't "visible" to Jersey. To make it visible, I create a MarshallerProvider. That looks something like this:

@Provider
public class ResourceJaxbMarshallerProvider implements ContextResolver<Marshaller> {
// injected by Spring
private ResourceJaxbFactory ResourceJaxbFactory;
private ResourceStatusJaxbFactory ResourceStatusJaxbFactory;


/*
 * ----------------------------------------
 * Setters (for Spring injected properties)
 * ----------------------------------------
 */
public void setResourceJaxbFactory(ResourceJaxbFactory ResourceJaxbFactory) {
    this.ResourceJaxbFactory = ResourceJaxbFactory;
}

public void setResourceStatusJaxbFactory(ResourceStatusJaxbFactory ResourceStatusJaxbFactory) {
    this.ResourceStatusJaxbFactory = ResourceStatusJaxbFactory;
}

/*
 * ------------------------
 * Interface Implementation
 * ------------------------
 */
public Marshaller getContext(Class<?> type) {
    if (type == ResourceDto.class)
        return ResourceJaxbFactory.getMarshaller();
    else if (type == ResourceStatusDto.class)
        return ResourceStatusJaxbFactory.getMarshaller();
    else
        return null;
}       
}

我使用Jersey将泽西连接到Spring / Spring Servlet所以由Spring创建的任何@Provider类都会被Jersey自动识别。在我的Spring applicationContext.xml中,我所要做的就是实例化资源提供者。反过来,它会从工厂中抓住编组人员。

I've got Jersey wired into Spring using the Jersey/Spring Servlet so any @Provider class that gets created by Spring is automatically recognized by Jersey. In my Spring applicationContext.xml all I have to do is instantiate the resource provider. It will, in turn, go grab the marshaller from the factory.

3)我发现另一件重要的事情是我必须创建一个package-info.java包含我的资源的根包中的文件。看起来像这样:

3) The other thing that I found critical was that I had to create a package-info.java file in the root package containing my resource. Looks like this:

/*
 * Note that this file is critical for ensuring that our ResourceDto object is
 * marshalled/unmarshalled with the correct namespace.  Without this, marshalled
 * classes produce XML files without a namespace identifier
 */
@XmlSchema(namespace = XmlMetadataConstants.XML_SCHEMA_NAMESPACE, elementFormDefault = XmlNsForm.QUALIFIED) 
package com.yourcompany.resource;

import javax.xml.bind.annotation.XmlNsForm;






至少我认为这就是我需要做的一切,我不记得每件作品。我确实记得,package-info.java是最后一个让它们聚集在一起的关键因素。


At least I think that's everything I needed to do, I can't remember every single piece. I do remember that the package-info.java piece was the last critical cog that made it all come together.

希望有所帮助。我花了太多时间来挖掘所有这些信息。在我希望它进行适当的xml架构验证(以及架构无效输入的正确错误报告)之前,Jersey很诱人。一旦我开始沿着这条路开始,泽西从脑死亡变得容易到相当困难。大多数困难是从网上各种帖子中删除所有细节。希望这将有助于您更快,更快。 : - )

Hope that helps. I spent wayyyy too much time digging for the info on all this. Jersey was seductively simple before I wanted it to do proper xml schema validation (and decent error reporting for schema-invalid input). Once I started down that road Jersey went from brain-dead easy to decently hard. The majority of that difficulty was sussing out all the details from the variety of posts online. Hopefully this will help get you farther, quicker. :-)

这篇关于如何自定义Jersey JAXB序列化的XML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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