### Complete REST API Example with DTOGen Source: https://context7.com/manoelcampos/dtogen/llms.txt This example demonstrates creating model entities, converting them to DTOs (excluding sensitive fields and mapping relationships to IDs), and creating DTOs directly for API requests, then converting them back to models for persistence. It requires model classes like LanguageClass, ContinentClass, CountryClass, ProfessionRecord, ReligionClass, and PersonRecord, along with a PersonRecordDTO. ```java package com.example; import com.example.model.*; public class Main { public static void main(String[] args) { // Create model entities LanguageClass language = new LanguageClass(1, "Brazilian Portuguese", "PT-BR"); ContinentClass continent = new ContinentClass("South America"); CountryClass country = new CountryClass(1, "Brazil", continent, 8_510_417_771L, language); ProfessionRecord profession = new ProfessionRecord(1, "Software Engineer", "IT"); ReligionClass religion = new ReligionClass("Christianity", "Middle East"); // Create parent entities PersonRecord mother = new PersonRecord(1, "Mother", 60, 35, "secret", country, profession, religion); PersonRecord father = new PersonRecord(2, "Father", 75, 40, "secret", country, profession, religion); // Create main entity with relationships PersonRecord person = new PersonRecord( 3, "John Doe", 80, 42, "mypassword", country, profession, religion, mother, father ); System.out.println("Original model: " + person); // Convert to DTO - password is excluded, country/profession/father are mapped to IDs PersonRecordDTO dto = new PersonRecordDTO().fromModel(person); System.out.println("DTO: " + dto); // DTO contains: id=3, name="John Doe", weightKg=80, footSize=42, // countryId=1, professionId=1, religion=..., mother=..., fatherId=2 // Note: password is EXCLUDED, country/profession/father are ID values only // Create DTO directly for API requests PersonRecordDTO requestDto = new PersonRecordDTO( 0, // new entity, no ID yet "Jane Doe", 65.0, 38, country.getId(), // countryId profession.id(), // professionId religion, null, // mother (full object, not mapped to ID) 0 // fatherId ); // Convert DTO to model for persistence PersonRecord newPerson = requestDto.toModel(); System.out.println("New model from DTO: " + newPerson); } } ``` -------------------------------- ### Using Lombok with DTOGen in Java Source: https://context7.com/manoelcampos/dtogen/llms.txt DTOGen integrates with Lombok-annotated classes. Lombok must be configured to run before DTOGen in the annotation processor chain. This example demonstrates model to DTO conversion. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @DTO @Data @AllArgsConstructor @NoArgsConstructor public class CountryClass { private static final long SOME_CONSTANT = 1; // Constants are excluded from DTO @NotNull private long id; @NotNull @NotBlank @Size(min = 10, max = 200) private String name; @NotNull private ContinentClass continent; private long areaKm2; @DTO.MapToId private LanguageClass language; } // Lombok generates getters/setters at compile time // DTOGen uses those getters in the generated fromModel() method // Build command: // mvn clean compile // Usage: CountryClass country = new CountryClass(); country.setId(1); country.setName("Brazil"); country.setContinent(new ContinentClass("South America")); country.setAreaKm2(8510417771L); country.setLanguage(new LanguageClass(1, "Portuguese", "PT-BR")); CountryClassDTO dto = new CountryClassDTO().fromModel(country); System.out.println(dto.name()); // "Brazil" System.out.println(dto.languageId()); // 1 ``` -------------------------------- ### Generated PersonDTO Record Source: https://github.com/manoelcampos/dtogen/blob/main/README.md This is an example of a generated `PersonDTO` record. It includes fields from the `Person` model and automatically generated `countryId` and `professionId` fields due to `@DTO.MapToId`. ```java /** * A {@link DTORecord Data Transfer Object} for {@link Person}. * @param professionId Current profession of the person */ @Generated public record PersonDTO ( long id, @NotNull @NotBlank String name, @DecimalMin(value="0.1") @DecimalMax(value="200") double weightKg, @Min(value=5) @Max(value=50) int footSize, long countryId, long professionId) implements DTORecord { @Override public Person toModel(){/*...*/} @Override public PersonDTO fromModel(Person model){/*...*/} } ``` -------------------------------- ### Generate DTO from Java Record with @DTO Source: https://context7.com/manoelcampos/dtogen/llms.txt Apply the @DTO annotation to a Java record to generate a corresponding DTO record. This example also demonstrates the use of @DTO.Exclude for sensitive fields and @DTO.MapToId for mapping related entities to their IDs. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import jakarta.validation.constraints.*; @DTO public record PersonRecord( long id, @NotNull @NotBlank String name, @DecimalMin("0.1") @DecimalMax("200") double weightKg, @Min(5) @Max(50) int footSize, @DTO.Exclude String password, @DTO.MapToId CountryClass country, @DTO.MapToId ProfessionRecord profession ) {} // Usage after build: PersonRecord person = new PersonRecord(1, "John Doe", 75.5, 42, "secret", country, profession); PersonRecordDTO dto = new PersonRecordDTO().fromModel(person); System.out.println(dto.name()); // "John Doe" System.out.println(dto.countryId()); // ID of the country object ``` -------------------------------- ### Gradle Configuration for DTOGen Source: https://context7.com/manoelcampos/dtogen/llms.txt Add DTOGen to your Gradle project using the implementation configuration. Ensure Lombok is configured as an annotation processor. ```groovy plugins { id 'java' } java { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } dependencies { implementation 'io.github.manoelcampos:dtogen:2.1.7' // Optional: Hibernate Validator implementation 'org.hibernate.validator:hibernate-validator:8.0.1.Final' // Optional: Lombok (must be processed before DTOGen) compileOnly 'org.projectlombok:lombok:1.18.36' annotationProcessor 'org.projectlombok:lombok:1.18.36' annotationProcessor 'io.github.manoelcampos:dtogen:2.1.7' } ``` -------------------------------- ### DTORecord Interface for Model Conversion Source: https://context7.com/manoelcampos/dtogen/llms.txt The DTORecord interface provides `toModel()` and `fromModel()` methods for bidirectional conversion between DTOs and model objects. Generated DTOs implement this interface. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import io.github.manoelcampos.dtogen.DTORecord; @DTO public record ProfessionRecord(long id, String name, String description) {} // The generated ProfessionRecordDTO implements DTORecord: /* public record ProfessionRecordDTO ( long id, String name, String description ) implements DTORecord { @Override public ProfessionRecord toModel() { return new ProfessionRecord(id, name, description); } @Override public ProfessionRecordDTO fromModel(ProfessionRecord model) { return new ProfessionRecordDTO ( model.id(), model.name(), model.description() ); } public ProfessionRecordDTO() { this(0, null, null); } } */ ``` ```java // Usage - Convert Model to DTO: ProfessionRecord profession = new ProfessionRecord(1, "Software Engineer", "IT"); ProfessionRecordDTO dto = new ProfessionRecordDTO().fromModel(profession); ``` ```java // Usage - Convert DTO back to Model: ProfessionRecordDTO dto = new ProfessionRecordDTO(2, "Data Scientist", "Analytics"); ProfessionRecord model = dto.toModel(); ``` -------------------------------- ### Include DTOGen Gradle Dependency Source: https://github.com/manoelcampos/dtogen/blob/main/README.md Add this dependency to your build.gradle file to include DTOGen in your Gradle project. Replace DTOGEN_VERSION_HERE with the desired version. ```groovy dependencies { //Set a specific version or use the latest one implementation 'io.github.manoelcampos:dtogen:DTOGEN_VERSION_HERE' } ``` -------------------------------- ### Maven Configuration for DTOGen Source: https://context7.com/manoelcampos/dtogen/llms.txt Add DTOGen as a dependency in your Maven project. Configure the annotation processor order to ensure Lombok runs before DTOGen if both are used. ```xml io.github.manoelcampos dtogen 2.1.7 org.hibernate.validator hibernate-validator 8.0.1.Final org.projectlombok lombok 1.18.36 provided org.apache.maven.plugins maven-compiler-plugin 3.13.0 21 21 org.projectlombok lombok 1.18.36 io.github.manoelcampos dtogen 2.1.7 -processor lombok.launch.AnnotationProcessorHider$AnnotationProcessor,io.github.manoelcampos.dtogen.DTOProcessor ``` -------------------------------- ### Generate DTO from Model Class with @DTO Source: https://context7.com/manoelcampos/dtogen/llms.txt Annotate a Java class with @DTO to automatically generate a corresponding DTO record. This includes copying fields, annotations (like validation constraints), and JavaDocs. The generated DTO implements `DTORecord` and provides `toModel()` and `fromModel()` methods. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import jakarta.validation.constraints.*; @DTO public class Person { private long id; @NotNull @NotBlank private String name; @DecimalMin("0.1") @DecimalMax("200") private double weightKg; @Min(5) @Max(50) private int footSize; // Getters and setters... } ``` ```java @Generated(value = "io.github.manoelcampos.dtogen.DTOProcessor", comments = "DTO generated using DTOGen Annotation Processor") public record PersonDTO ( long id, @NotNull @NotBlank String name, @DecimalMin(value="0.1") @DecimalMax(value="200") double weightKg, @Min(value=5) @Max(value=50) int footSize ) implements DTORecord { @Override public Person toModel() { ... } @Override public PersonDTO fromModel(Person model) { ... } public PersonDTO() { ... } } ``` -------------------------------- ### Include DTOGen Maven Dependency Source: https://github.com/manoelcampos/dtogen/blob/main/README.md Add this dependency to your pom.xml to include DTOGen in your Maven project. Replace DTOGEN_VERSION_HERE with the desired version. ```xml io.github.manoelcampos dtogen DTOGEN_VERSION_HERE ``` -------------------------------- ### Annotate Model Class with @DTO Source: https://github.com/manoelcampos/dtogen/blob/main/README.md Annotate your model class with `@DTO` to generate a corresponding DTO record. Validation annotations like `@NotNull` and `@NotBlank` are automatically copied to the DTO. ```java @DTO public class Person { private long id; @NotNull @NotBlank private String name; @DecimalMin("0.1") @DecimalMax("200") private double weightKg; @Min(5) @Max(50) private int footSize; @DTO.Ignore private String password; @DTO.MapToId private Country country; /** Current profession of the person */ @DTO.MapToId private Profession profession; } ``` -------------------------------- ### Configure DTO Generation with @DTO.Exclude and @DTO.MapToId Source: https://github.com/manoelcampos/dtogen/blob/main/README.md Use `@DTO.Exclude` to omit a field from the DTO. Use `@DTO.MapToId` on a field of a model class type to include only its ID in the DTO. ```java @DTO.Exclude private String password; ``` ```java @DTO.MapToId private Country country; ``` -------------------------------- ### Exclude Fields from DTO Generation with @DTO.Exclude Source: https://context7.com/manoelcampos/dtogen/llms.txt Use the @DTO.Exclude annotation on fields within a model class to prevent them from being included in the generated DTO. This is useful for sensitive data like passwords or internal identifiers. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; @DTO public class User { private long id; @NotNull @NotBlank private String username; @NotNull @NotBlank private String email; @DTO.Exclude private String password; @DTO.Exclude private String securityToken; @DTO.Exclude private String internalNotes; // Getters and setters... } // Generated UserDTO will NOT contain password, securityToken, or internalNotes fields: /* public record UserDTO ( long id, @NotNull @NotBlank String username, @NotNull @NotBlank String email ) implements DTORecord { ... } */ ``` -------------------------------- ### Map Entity References to IDs with @DTO.MapToId Source: https://context7.com/manoelcampos/dtogen/llms.txt Use @DTO.MapToId to convert a field referencing another entity object into just its ID value. This reduces DTO payload size and avoids circular references. The generated DTO will contain a field named 'entityNameId'. ```java package com.example.model; import io.github.manoelcampos.dtogen.DTO; import jakarta.validation.constraints.*; @DTO public class CountryClass { @NotNull private long id; @NotNull @NotBlank @Size(min = 10, max = 200) private String name; @NotNull private ContinentClass continent; private long areaKm2; @DTO.MapToId private LanguageClass language; // Getters and setters... } @DTO public class LanguageClass { private long id; private String name; private String code; // Getters and setters... } ``` ```java // Generated CountryClassDTO: /* public record CountryClassDTO ( @NotNull long id, @NotNull @NotBlank @Size(min=10, max=200) String name, @NotNull ContinentClass continent, long areaKm2, long languageId // <-- MapToId converts LanguageClass to languageId ) implements DTORecord { ... } */ ``` ```java // Usage: LanguageClass language = new LanguageClass(1, "Portuguese", "PT-BR"); CountryClass country = new CountryClass(1, "Brazil", continent, 8510417771L, language); CountryClassDTO dto = new CountryClassDTO().fromModel(country); System.out.println(dto.languageId()); // 1 (just the ID, not the full object) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.