EasyDto is java based library for working with DTOs (data transfer objects) of domain objects without requiring to write any Dto class or mapping logic explicitly
repositories {
maven { url = uri("https://nexus.xo-6.studio/repository/maven-public/") }
}
dependencies {
implementation("org.easybird:easy-dto-core:latest")
}
Original repository: EasyDto
- Multi- Superclass support
- Support for the java util List<?>
public class Student {
@DtoProperty
public String name;
@DtoProperty(value = "dept")
public Department department;
}
Also, the field name in DTO can be customized in the annotation, otherwise the field name is used.
Properties can also be derived for DTO. In this case annotate a getter
with @DtoProperty
annotation.
@DtoProperty("isEnrolled")
public boolean hasDepartment(){
return department != null;
}
Often we need different types of a single domain object depending on requirement. That can addressed here using
profile
. Simply declare in the annotation in which profile the field should be present in the DTO. No profile
declaration means all profiles. All properties are picked up when no profile is used during conversion.
public class Student {
@DtoProperty(profile = {"WEB"})
public String name;
}
For a domain object of say Student
class, a DTO can simply be created as following and the mapping is done for
you under the hood.
Student student = new Student("John", new Department(1, "CST"));
Dto<Student> dto = Dto.from(student);
Of course, the target profile can be provided and even a custom DtoConverter
instance can be passed. Otherwise the
default is used.
Dto<Student> dto = Dto.from(student, "REST");
Simply pass an instance of the target domain object to the DTO to map. This is type-safe.
Dto<Student> dto = deserialize();// some deserialzation logic
Student student = new Student();
dto.map(student);
Similary profile and custom DtoDeConverter
can be provided.
One can not work with DTOs without some sort of serialization. Currently, there is library support for jackson library only i.e., DTOs can be serialized/deserialized to json. Support for other formats can be extended.
Create an ObjectMapper
instance and register the out-of-the-box serialization module.
ObjectMapper mapper = new ObjectMapper();
Registerer.registerModules(mapper);
As long as you are using Jackson for serialization or write your own support for your format, you can use it seamlessly with Spring.
@Autowired
private ObjectMapper mapper;
// ....
Registerer.registerModules(mapper);
Simply autowire
the object mapper, the one spring has created and will use internally, and register the modules. And
you can run your spring application without writing the DTO class or any mapping logic of any sort. Following is a
sample Spring Rest Endpoint.
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/{id}")
public Dto<Student> get(@PathVariable Long id) {
Dto<Student> dto = Dto.from(studentService.getStudent(id));
return dto;
}
@PostMapping
public void post(@RequestBody Dto<Student> dto) {
Student newStudent = new Student();
dto.map(newStudent);
studentService.saveStudent(newStudent);
}
}
clone this repository and build locally to install in local .m2. Currently, only maven is supported.
mvn clean install
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.