JAVA Bean 轉換工具 BULL 使用簡介

How to Transform Any Type of Java Bean With BULL

在跨團隊或者跨系統的開發調用時,經常遇到 兩個系統Java 代碼命名不一致的情況,簡單直接的辦法就是寫一堆get set

如果能拿到對方的jar 或者源碼 亦可以使用對方的命名:

例如

對店鋪的定義

A系統

public class shop{
	private long id ;
	private String name;
	private String address;
	}

B系統

public class  store{
	private Long  storeId;
	private String name;
	private String location;
	}

BULL 的使用

一、maven 的引入

The project provides two different builds, one compatible with jdk 8 (or above) and one with jdk 11 or above.

BULL提供了兩個版本的jar
一個基於JDK8

一個基於JDK11

<dependency>
    <groupId>com.hotels.beans</groupId>
    <artifactId>bull-bean-transformer</artifactId>
    <version>1.7.1</version>
</dependency>

二、特性

The macro features explained in this article are:

JAVA 對象轉換 Bean transformation
JAVA 對象驗證 Bean validation

三、對象轉換

3.1

例如 給出兩個不同的bean

public class FromBean {                                     public class ToBean {                           
   private final String name;                                  public BigInteger id;                                  
   private final BigInteger id;                                private final String name;                             
   private final List<FromSubBean> subBeanList;                private final List<String> list;                       
   private List<String> list;                                  private final List<ImmutableToSubFoo> nestedObjectList;
   private final FromSubBean subObject;                        private ImmutableToSubFoo nestedObject;                
    // all args constructor                                     // constructors                                         
   // getters and setters...                                    // getters and setters
}                                                           }

The transformation can be obtained with the following line of code:

ToBean toBean = new BeanUtils().getTransformer().transform(fromBean, ToBean.class);

簡單的不同名稱,成員變量同名的對象轉換,spring 框架也也提供了類似功能。

3.2 不同成員名稱的對象轉換

Different Field Names Copy

需要組裝一個map ,定義好轉換的對應關係。

public class FromBean {                                     public class ToBean {                           
   private final String name;                                  private final String differentName;                   
   private final int id;                                       private final int id;                      
   private final List<FromSubBean> subBeanList;                private final List<ToSubBean> subBeanList;                 
   private final List<String> list;                            private final List<String> list;                    
   private final FromSubBean subObject;                        private final ToSubBean subObject;                    
   // all constructors                                         // all args constructor
   // getters...                                               // getters... 
}                                                            }

We need to define proper field mappings and pass it to theTransformer object:

// the first parameter is the field name in the source object
// the second one is the the field name in the destination one 
FieldMapping fieldMapping = new FieldMapping("name", "differentName");
Tansformer transformer = new BeanUtils().getTransformer().withFieldMapping(fieldMapping);

Then, we can perform the transformation:

ToBean toBean = transformer.transform(fromBean, ToBean.class);

3.3 不同層級的數據轉換

FromBean 有一個成員也是 Bean 對象

需要FromsubBean 成員 轉換成ToBean 成員變量

public class FromSubBean {                         
   private String serialNumber;                 
   private Date creationDate;                    
   // getters and setters... 
}

and our source class and destination class are described as follow:

public class FromBean {                                     public class ToBean {                           
   private final int id;                                       private final int id;                      
   private final String name;                                  private final String name;                   
   private final FromSubBean subObject;                        private final String serialNumber;                 
                                                               private final Date creationDate;                    
   // all args constructor                                     // all args constructor
   // getters...                                               // getters... 
}                                                           }

…and that the values for fields serialNumber and creationDate into the ToBean object need to be retrieved from subObject, this can be done defining the whole path to the property dot separated:

FieldMapping serialNumberMapping = new FieldMapping("subObject.serialNumber", "serialNumber");                                                             
FieldMapping creationDateMapping = new FieldMapping("subObject.creationDate", "creationDate");
ToBean toBean = new BeanUtils().getTransformer()
                   .withFieldMapping(serialNumberMapping, creationDateMapping)
                   .transform(fromBean, ToBean.class);

3.4 使用構造方法

Different Field Names Defining Constructor Args
The mapping between different fields can also be defined by adding @ConstructorArg annotation next to constructor arguments.

The @ConstructorArg takes as input the name of the correspondent field in the source object.

public class FromBean {                                     public class ToBean {                           
   private final String name;                                  private final String differentName;                   
   private final int id;                                       private final int id;                      
   private final List<FromSubBean> subBeanList;                private final List<ToSubBean> subBeanList;                 
   private final List<String> list;                            private final List<String> list;                    
   private final FromSubBean subObject;                        private final ToSubBean subObject;                    
   // all args constructor
   // getters...                                                                }
     
}
subBeanList                                                       

public ToBean(@ConstructorArg("name") final String differentName, 
 @ConstructorArg("id") final int id,
@ConstructorArg("subBeanList") final List<ToSubBean> 
 @ConstructorArg(fieldName ="list") final List<String> list,
 @ConstructorArg("subObject") final ToSubBean subObject) {
 this.differentName = differentName;
this.id = id;
 this.subBeanList = subBeanList;
 this.list = list;
this.subObject = subObject; 
 }
 // getters...           

參考資料:https://dzone.com/articles/how-to-transform-any-type-of-java-bean-with-one-li

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章