Skip to content

Latest commit

 

History

History
119 lines (99 loc) · 2.47 KB

File metadata and controls

119 lines (99 loc) · 2.47 KB

Generate DTO and Configuration ✨

@dto Attribute

Key Behaviors

1. Automatic Suffixing

@HyperResource() // Becomes "ProductDTO"
public class Product{
    //...
}

2. Custom naming

@HyperResource( dto = "SingleProduct" ) // Becomes "SingleProductDTO"
public class Product{
    //...
}

Default Naming Convention

@HyperResource // No DTO specified
public class User {
    //...
}

➡️ Generated DTO: UserDTO (Entity name + "DTO")

Custon DTO Name

@HyperResource(dto = "AccountResponse")
public class User {
    //...
}

➡️ Generated DTO: AccountResponseDTO

Field Inclusion Rule (for JSON)

@Entity
@HyperResource(dto = "MyProduct")
public class Product {
    String name;          // Included
    BigDecimal price;     // Included
    
    @JsonbTransient
    List<Order> orders;   // Excluded
}

Generated DTO example

// For @HyperResource( dto = "MyProduct" )
@JsonInclude(Include.NON_NULL)
public class MyProductDTO extends BaseDTO {
    @JsonProperty("name")
    private String name;
    @JsonProperty("price")
    private BigDecimal price;
    @JsonProperty("orders")
    private List<Order> orders = new ArrayList();

    public MyProductDTO() {
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public List<Order> getOrders() {
        return this.orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public void addOrdersItem(Order item) {
        this.orders.add(item);
    }

    public void clearOrders() {
        this.orders.clear();
    }

    public String toString() {
        return String.format("MyProductDTO [name=%s, price=%s, orders=%s]", this.name, this.price, this.orders);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            MyProductDTO that = (MyProductDTO)o;
            return Objects.equals(this.name, that.name) && Objects.equals(this.price, that.price) && Objects.equals(this.orders, that.orders);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.name, this.price, this.orders});
    }
}