Can I get MOXy to not output an attribute when generating json?

后端 未结 1 1330
暗喜
暗喜 2021-01-25 15:22

An instance of my JAXB Object model contains an attribute that I want output when I generate Xml for the instance but not when I generate json

i.e I want



        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 16:15

    Since your JSON binding is slightly different from your XML binding I would use EclipseLink JAXB (MOXy)'s external mapping file.

    oxm.xml

    In the external mapping file we will mark the type field as transient.

    
    
        
            
                
                    
                
            
        
    
    

    ReleaseGroup

    Below is the domain model I'll use for this example. Note how the type property is annotated with @XmlAttribute.

    package forum383861;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="release-group")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ReleaseGroup {
    
        @XmlAttribute
        String type;
    
        String title;
    
    }
    

    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: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

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

    Demo

    Since the XML and JSON representations are different we'll create separate JAXBContexts for them. For the JSON one we'll leverage MOXy's external mapping file.

    package forum383861;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            ReleaseGroup rg = new ReleaseGroup();
            rg.type = "Album";
            rg.title = "Fred";
    
            // XML
            JAXBContext xmlJC = JAXBContext.newInstance(ReleaseGroup.class);
            Marshaller xmlMarshaller = xmlJC.createMarshaller();
            xmlMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            xmlMarshaller.marshal(rg, System.out);
    
            // JSON
            Map properties = new HashMap(2);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum383861/oxm.xml");
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {ReleaseGroup.class}, properties);
            Marshaller jsonMarshaller = jsonJC.createMarshaller();
            jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jsonMarshaller.marshal(rg, System.out);
        }
    
    }
    

    Output

    Below is the output from running the demo code:

    
    
       Fred
    
    {
       "release-group" : {
          "title" : "Fred"
       }
    }
    

    0 讨论(0)
提交回复
热议问题