modelmapper

how to exclude whole property if they are null from Modelmapper

自古美人都是妖i 提交于 2019-12-06 12:31:24
问题 Does ModelMapper(http://modelmapper.org/) support what exclude property? If the value is null. I just found PropertyMap out. but It is a constraint to me. because I have to describe a specific property that I want. Like this. ModelMapper modelMapper = new ModelMapper(); modelMapper.addMappings(new PropertyMap<TestObject, TestObject>() { @Override protected void configure() { when(Conditions.isNull()).skip().setName(source.getName()); when(Conditions.isNull()).skip().set...(source.get...());

Modelmapper to convert from String to LocalDate

雨燕双飞 提交于 2019-12-05 09:14:56
My DTO is having date field in String format. My entity is having date as LocalDate. Currently I am skipping it from map and then later manually explicitly setting it (String to Date and vis-versa). is it possible to convert it automatically? I tried Converter inside spring bean but it gives me lot of compile errors (type Converter does not take parameters, does not override convert method - also lot of error for convert() as well). @Bean public ModelMapper studentModelMapper() { .... Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() { protected String

5种常见Bean映射工具的性能比对

点点圈 提交于 2019-12-04 11:54:02
本文由 JavaGuide 翻译自 https://www.baeldung.com/java-performance-mapping-frameworks 。转载请注明原文地址以及翻译作者。 1. 介绍 创建由多个层组成的大型 Java 应用程序需要使用多种领域模型,如持久化模型、领域模型或者所谓的 DTO。为不同的应用程序层使用多个模型将要求我们提供 bean 之间的映射方法。手动执行此操作可以快速创建大量样板代码并消耗大量时间。幸运的是,Java 有多个对象映射框架。在本教程中,我们将比较最流行的 Java 映射框架的性能。 综合日常使用情况和相关测试数据,个人感觉 MapStruct、ModelMapper 这两个 Bean 映射框架是最佳选择。 2. 常见 Bean 映射框架概览 2.1. Dozer Dozer 是一个映射框架,它使用递归将数据从一个对象复制到另一个对象。框架不仅能够在 bean 之间复制属性,还能够在不同类型之间自动转换。 要使用 Dozer 框架,我们需要添加这样的依赖到我们的项目: <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency> 更多关于 Dozer

Spring REST API 从实体到 DTO 的转换

本小妞迷上赌 提交于 2019-12-04 06:17:49
介绍 在本文中,我们将处理 Spring 应用的内部实体与客户端外的 DTO(数据传输对象)之间需要进行的转换。 模型映射 让我们从介绍用于执行的实体到 DTO 转换的主库开始 —— ModelMapper。 我们需要将在 pom.xml 中添加如下依赖: <dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>2.3.2</version> </dependency> 然后,我们在 Spring 配置中定义 ModelMapper bean: @Bean public ModelMapper modelMapper() { return new ModelMapper(); } DTO 接下来,让我们来介绍这个双面问题的 DTO 方面 —— Post DTO: public class PostDto { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private Long id; private String title; private String url; private String date;

How to customize ModelMapper

半腔热情 提交于 2019-12-03 18:37:57
问题 I want to use ModelMapper to convert entity to DTO and back. Mostly it works, but how do I customize it. It has has so many options that it's hard to figure out where to start. What's best practice? I'll answer it myself below, but if another answer is better I'll accept it. 回答1: First here are some links modelmapper getting started api doc blog post random code examples My impression of mm is that it is very well engineered. The code is solid and a pleasure to read. However, the

Better way to map Kotlin data objects to data objects

大城市里の小女人 提交于 2019-12-02 16:08:19
I want to convert/map some "data" class objects to similar "data" class objects. For example, classes for web form to classes for database records. data class PersonForm( val firstName: String, val lastName: String, val age: Int, // maybe many fields exist here like address, card number, etc. val tel: String ) // maps to ... data class PersonRecord( val name: String, // "${firstName} ${lastName}" val age: Int, // copy of age // maybe many fields exist here like address, card number, etc. val tel: String // copy of tel ) I use ModelMapper for such works in Java, but it can't be used because

ModelMapper: Ensure that method has zero parameters and does not return void

a 夏天 提交于 2019-12-01 04:26:07
I have the following configuration for the model mapper to convert an instance of User class to an instance of ExtendedGetUserDto . public ExtendedGetUserDto convertToExtendedDto(User user) { PropertyMap<User, ExtendedGetUserDto> userMap = new PropertyMap<User, ExtendedGetUserDto>() { protected void configure() { map().setDescription(source.getDescription()); map().setId(source.getId()); // map().setReceivedExpenses( // source.getReceivedExpenses() // .stream() // .map(expense -> expenseDtoConverter.convertToDto(expense)) // .collect(Collectors.toSet()) // ); Set<GetInvitationDto> result = new

ModelMapper: Ensure that method has zero parameters and does not return void

こ雲淡風輕ζ 提交于 2019-12-01 00:53:34
问题 I have the following configuration for the model mapper to convert an instance of User class to an instance of ExtendedGetUserDto . public ExtendedGetUserDto convertToExtendedDto(User user) { PropertyMap<User, ExtendedGetUserDto> userMap = new PropertyMap<User, ExtendedGetUserDto>() { protected void configure() { map().setDescription(source.getDescription()); map().setId(source.getId()); // map().setReceivedExpenses( // source.getReceivedExpenses() // .stream() // .map(expense ->

ModelMapper, mapping list of Entites to List of DTO objects

给你一囗甜甜゛ 提交于 2019-11-30 23:18:15
I am writing simple blog web application using Spring MVC framework. I am willing to add DTO layer to my app. I decided to use ModelMapper framework for conversion from Entity objects to DTO objects used in my views. I have just one problem. On my main page, I am showing a list of posts on my blog. In my view, it's just list of Post (Entity) objects. I want to change it to pass a list of PostDTO objects to my view. Is there any way to map List of Post objects to List of PostDTO object with single method call? I was thinking about writing converter that will convert this but I am not sure it's

How to use Explicit Map with Java 8 and ModelMapper?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:55:40
I learn how to use ModelMapper by official documentation http://modelmapper.org/getting-started/ There is code sample for explicit mapping using java 8 modelMapper.addMappings(mapper -> { mapper.map(src -> src.getBillingAddress().getStreet(), Destination::setBillingStreet); mapper.map(src -> src.getBillingAddress().getCity(), Destination::setBillingCity); }); How to use this code correctly? When I type this code snippet in IDE, IDE show me message cannot resolve method map They missed a step in this example, the addMappings method they use is the addMappings from TypeMap , not from ModelMapper