This library allows you to convert your entities to a selected DTO class in a very easy way.
-
Download last version of Jar from Release page
-
Create configuration file and paste code below
@Configuration
@ComponentScan("com.mapperDTO")
public class ApplicationConfig {
@Bean
public EntityMapper newEntityMapper(){
return new EntityMapper();
}
@Bean
public EntityMapperInvoker<Object, Object> newEntityMapperInvoker(){
return new EntityMapperInvoker<>();
}
}
- Autowire EntityMapperInvoker into your service
@Autowired
private EntityMapperInvoker<YourEntity, YourDTO> entityMapperInvoker;
- Mark the fields in the DTO that you want to be mapped using
@MapToDTO(mapClass = {View.TripView.class, View.SeatTripView.class})
private long id;
mapClass is needed to avoid loops that work like JsonView
- Mark the DTO class
@DTO
public class CityDTO {
//your code
}
- And in your method just add next line
return entityMapperInvoker.entityToDTO(entity object, DTO.class, mapClass from the annotation);
DTO class must contain constructor without any arguments
If the DTO class contains fields of type DTO - these classes should also be marked as @DTO
Use the "associatedField" property in the @MapToDTO
annotation if your variable in the DTO has a different name than in the Entity