Dozer is a Java bean to Java Bean Mapper This recursively copies data from one object to another. Typically, these Java Beans would be the different complex types.
For more information, see: http://dozer.sourceforge.net/documentation/about.html
Two simple examples:
-
Map-to-bean
assumes that the page parameter map was obtained by request and wants to convert it to a domain object –product.
Product has several properties:
private Long ID;
private String name;
Private String description;
Code:
//map--> Bean
map<string,object> map = Maps.newhashmap ();
Map.put ("id", 10000L);
Map.put ("name", "Da");
Map.put ("description", "Gold shell");
dozerbeanmapper mapper = new Dozerbeanmapper ();
Product Product = Mapper.map (map, Product.class);
Assertthat (Product.getid ()). Isequalto (10000L);
Assertthat (Product.getname ()). Isequalto ("the clatter");
Assertthat (Product.getdescription ()). Isequalto ("Gold shell");
2. Bean-to-DTO
For the sake of decoupling, a separate DTO object is created for the interface provided to the third party, rather than the entity object that has the existing corresponding database table. It is necessary to transfer data between them.
If a Dto object is now defined, but the property name does not match the property name in the Product object (perhaps to avoid conflicts or ambiguities), as follows:
Private long productId;
Private String ProductName;
Private String desc;
Simply add an annotation to the relevant attribute, as shown below:
@Mapping ("id")
Private long productId;
@Mapping ("name")
Private String ProductName;
@Mapping ("description")
Private String desc;
Complete code example see:
Product Product = new product ();
Product.setid (10001L);
Product.setname ("Hero");
Product.setdescription ("Black Shell");
Dozerbeanmapper mapper = new Dozerbeanmapper ();
Productdto productdto = Mapper.map (product, Productdto.class);
Assertthat (Productdto.getproductid ()). Isequalto (10001L);
Assertthat (Productdto.getproductname ()). Isequalto ("Hero");
Assertthat (Productdto.getdesc ()). Isequalto ("Black Shell");
A powerful and easy-to-use Java Bean attribute replication Framework--dozer Introduction