Search code examples
spring-data-rest

How are order line items exposed?


In the restbucks project, an order has a private list of line items. I can't figure out how this field is exposed via rest api when getting a single order. It has no public getter or anything. Is it the mixins?


Solution

  • @Data, @NoArgsConstructor, @AllArgsConstructor, @EqualsAndHashCode are Lombok annotations, that are used to auto-generate a boilerplate code.

    @Data
    @Entity
    @NoArgsConstructor(force = true)
    @AllArgsConstructor
    @EqualsAndHashCode(callSuper = false)
    public class LineItem extends AbstractEntity {
    
      private final String name;
      private final int quantity;
      private final Milk milk;
      private final Size size;
      private final MonetaryAmount price;
    
      public LineItem(String name, MonetaryAmount price) {
        this(name, 1, Milk.SEMI, Size.LARGE, price);
      }
    }
    

    You can compare this code with delomboked one (welcome to Lombok-funs club )):

    @Entity
    public class LineItem extends AbstractEntity {
    
        private final String name;
        private final int quantity;
        private final Milk milk;
        private final Size size;
        private final MonetaryAmount price;
    
        public LineItem(String name, MonetaryAmount price) {
            this(name, 1, Milk.SEMI, Size.LARGE, price);
        }
    
        public LineItem(String name, int quantity, Milk milk, Size size, MonetaryAmount price) {
            this.name = name;
            this.quantity = quantity;
            this.milk = milk;
            this.size = size;
            this.price = price;
        }
    
        public LineItem() {
            this.name = null;
            this.quantity = 0;
            this.milk = null;
            this.size = null;
            this.price = null;
        }
    
        public String getName() {
            return this.name;
        }
    
        public int getQuantity() {
            return this.quantity;
        }
    
        public Milk getMilk() {
            return this.milk;
        }
    
        public Size getSize() {
            return this.size;
        }
    
        public MonetaryAmount getPrice() {
            return this.price;
        }
    
        public String toString() {
            return "LineItem(name=" + this.getName() + ", quantity=" + this.getQuantity() + ", milk=" + this.getMilk() + ", size=" + this.getSize() + ", price=" + this.getPrice() + ")";
        }
    
        public boolean equals(Object o) {
            if (o == this) return true;
            if (!(o instanceof LineItem)) return false;
            final LineItem other = (LineItem) o;
            if (!other.canEqual((Object) this)) return false;
            final Object this$name = this.getName();
            final Object other$name = other.getName();
            if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
            if (this.getQuantity() != other.getQuantity()) return false;
            final Object this$milk = this.getMilk();
            final Object other$milk = other.getMilk();
            if (this$milk == null ? other$milk != null : !this$milk.equals(other$milk)) return false;
            final Object this$size = this.getSize();
            final Object other$size = other.getSize();
            if (this$size == null ? other$size != null : !this$size.equals(other$size)) return false;
            final Object this$price = this.getPrice();
            final Object other$price = other.getPrice();
            if (this$price == null ? other$price != null : !this$price.equals(other$price)) return false;
            return true;
        }
    
        public int hashCode() {
            final int PRIME = 59;
            int result = 1;
            final Object $name = this.getName();
            result = result * PRIME + ($name == null ? 43 : $name.hashCode());
            result = result * PRIME + this.getQuantity();
            final Object $milk = this.getMilk();
            result = result * PRIME + ($milk == null ? 43 : $milk.hashCode());
            final Object $size = this.getSize();
            result = result * PRIME + ($size == null ? 43 : $size.hashCode());
            final Object $price = this.getPrice();
            result = result * PRIME + ($price == null ? 43 : $price.hashCode());
            return result;
        }
    
        protected boolean canEqual(Object other) {
            return other instanceof LineItem;
        }
    }