'Get Derived DTO From Base Class Request Body DTO

I try to get derived class fields from methods response body. Request body parameter is type of base class. Request comes with derived class fields but I can't cast it to derived class.

Here is my controller method and DTO classes:

Method:

 @PostMapping("/{code}")
    public ResponseEntity<PromotionDto> createPromotion(@PathVariable String code, @RequestBody PromotionDto promotion){
        if(PromotionTypeEnum.ORDER_THRESHOLD_DISCOUNT.equals(promotion.getPromotionType())) {
            promotionService.createPromotion(orderThresholdDiscountPromotionConverter.toEntity((OrderThresholdDiscountPromotionDto)promotion));
        }
        return ResponseEntity.ok(promotion);
    }

Base class DTO:

import dto.base.BaseDto;
import promotionservice.PromotionTypeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class PromotionDto extends BaseDto {
    private String code;
    private String title;
    private String description;
    private PromotionTypeEnum promotionType;

}

Derived class DTO:

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class OrderThresholdDiscountPromotionDto extends PromotionDto {
    private Double thresholdTotal;
    private Double discountPrice;
    private String messageFired;
    private String messageCouldHaveFired;
}

Request JSON is:

{
    "code":"qwewe",
    "title":"qwewe",
    "description":"qwewe",
    "promotionType":"ORDER_THRESHOLD_DISCOUNT",
    "thresholdTotal":1.3,
    "discountPrice":"12.5",
    "messageFired":"qwewe",
    "messageCouldHaveFired":"qwewe"

}

as result, service returns error:

{
"type": "https://www.jhipster.tech/problem/problem-with-message",
"title": "Internal Server Error",
"status": 500,
"detail": "promotion.PromotionDto cannot be cast to promotion.OrderThresholdDiscountPromotionDto",
"path": "/api/promotionresults/qwewe",
"message": "error.http.500"

}

My question is: is there any way, library, annotation etc. to get the derived class instance from request ?



Solution 1:[1]

What are you trying to do is you are trying typecast the Parent into a child which is known as Downcasting. This is only valid when you have the Parent as an instance of child. In your case, PromotionDto should be an instance of OrderThresholdDiscountPromotionDto.

Please refer below example:

public class PromotionDto  {
    private String code;
    private String title;
    private String description;




    public static void main(String[] args) {

      PromotionDto promotionDto = new OrderThresholdDiscountPromotionDto();
      PromotionDto promotionDto_2 = new PromotionDto();

    //Valid downcasting
      OrderThresholdDiscountPromotionDto subClass1 = (OrderThresholdDiscountPromotionDto)promotionDto;

      //Invalid down casting
      OrderThresholdDiscountPromotionDto subClass2 = (OrderThresholdDiscountPromotionDto)promotionDto_2;

    }


}

class OrderThresholdDiscountPromotionDto extends PromotionDto {
  private Double thresholdTotal;
  private Double discountPrice;
  private String messageFired;
  private String messageCouldHaveFired;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sujit kumar