Can I get MOXy to rename an element when generatin

2019-03-01 00:40发布

From a common JAXB model the xml generated can be of the form

<ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list>

because in json we have arrays we dont need both elements, so by using MOXy's oxml extensions I can flatten the output to give

"ipi" : [ "1001", "1002" ],

but because ipi now refers to an array of things I would like it to be called ipis not ipi

"ipis" : [ "1001", "1002" ],

Is there a way to get MOXy to rename an element ?

1条回答
不美不萌又怎样
2楼-- · 2019-03-01 01:43

You could use EclipseLink JAXB (MOXy)'s external mapping document to tweak the mapping for either the XML or JSON representation.

IPIList

Below is a domain class with JAXB annotations that matches the XML representation from your question:

package forum11449219;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="ipi-list")
public class IPIList {

    private List<String> list = new ArrayList<String>();

    @XmlElement(name="ipi")
    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

}

oxm.xml

We can use MOXy's external mapping document to modify how the list property is mapped to JSON.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11449219">
    <java-types>
        <java-type name="IPIList">
            <java-attributes>
                <xml-element java-attribute="list" name="ipis"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see ):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

The following demo code shows how to reference the external mapping document when creating a JAXBContext.

package forum11449219;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        IPIList ipiList = new IPIList();
        ipiList.getList().add("1001");
        ipiList.getList().add("1002");

        // XML
        JAXBContext jc = JAXBContext.newInstance(IPIList.class);
        Marshaller xmkMarshaller = jc.createMarshaller();
        xmkMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        xmkMarshaller.marshal(ipiList, System.out);

        // JSON
        Map<String, Object> properties = new HashMap<String, Object>(3);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11449219/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {IPIList.class}, properties);
        Marshaller jsonMarshaller = jsonJC.createMarshaller();
        jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jsonMarshaller.marshal(ipiList, System.out);
    }

}

Output

Here is the output from running the demo code:

<?xml version="1.0" encoding="UTF-8"?>
<ipi-list>
   <ipi>1001</ipi>
   <ipi>1002</ipi>
</ipi-list>
{
   "ipis" : [ "1001", "1002" ]
}

For More Information

查看更多
登录 后发表回答