MOXy是否可以仅使用吸气剂对POJO进行序列化而无需在每个吸气剂上显式放置注释? [英] Can MOXy serialize POJO with getters only without need to explicitly put annotation at each getter?

查看:80
本文介绍了MOXy是否可以仅使用吸气剂对POJO进行序列化而无需在每个吸气剂上显式放置注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定我的类名:

class Name {
  private String first;
  private String middle;
  private String last;

  public getFirst() { return first; }
  public getMiddle() { return middle; }
  public getLast() { return last; }
}

我想使用映射XML序列化此类的实例,而不必以列出映射XML中的每个属性:

I would like to serialize instances of this class using the mapping XML without having to list each property in the mapping XML:

<java-types>
    <java-type name="Name">
        <java-attributes>
           <xml-element java-attribute="first"/>
           <xml-element java-attribute="middle"/>
           <xml-element java-attribute="last"/>
        </java-attributes>
    </java-type>
</java-types> 

所以理想情况下,我希望具有这样的映射文件:

So ideally I would like to have mapping file like this:

<java-types>
    <java-type name="Name" xml-accessor-type="GETTERS"/>
</java-types> 

我有一些类似的DTO类,仅用于序列化(无意使用setter),其值为30或更多属性,理想情况下,我希望避免列出映射文件中的每个属性。

I have some legacy DTO classes like this intended for for serialization only (no setters intentionally) with 30 or more properties and ideally I would want to avoid listing each single property in the mapping file.

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 的负责人以及 JAXB 2(JSR-222)的成员 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

对于此用例,我建议使用字段访问类型。指定此选项后,JAXB实现将使用字段(实例变量)访问数据,而不是通过属性(get方法)进行访问。下面,我将演示如何使用MOXy的外部映射文档扩展名:

For this use case I would recommend using the field access type. When this is specified the JAXB implementation will use the fields (instance variables) to access the data instead of going through the property (get method). Below I'll demonstrate how to do this using MOXy's external mapping document extension:

bindings.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10141543">
    <java-types>
        <java-type name="Name" xml-accessor-type="FIELD">
            <xml-root-element/>
        </java-type>
    </java-types>
</xml-bindings>

名称

package forum10141543;

class Name {
    private String first;
    private String middle;
    private String last;

    public Name() {
    }

    public Name(String first, String middle, String last) {
        this.first = first;
        this.middle = middle;
        this.last = last;
    }

    public String getFirst() { return first; }
    public String getMiddle() { return middle; }
    public String getLast() { return last; }
}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域类相同的包中添加一个名为 jaxb.properties 的文件,

To specify MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain classes with the following entry.

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

演示

以下代码演示了引导 JAXBContext 时如何传递绑定文件:

The following code demonstrates how to pass in the bindings file when bootstrapping the JAXBContext:

package forum10141543;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum10141543/bindings.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Name.class}, properties);

        Name name = new Name("Jane", "Anne", "Doe");
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(name, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<name>
   <first>Jane</first>
   <middle>Anne</middle>
   <last>Doe</last>
</name>

了解更多信息

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html

这篇关于MOXy是否可以仅使用吸气剂对POJO进行序列化而无需在每个吸气剂上显式放置注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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