从基础 Class 请求正文 DTO 中获取派生 DTO

Get Derived DTO From Base Class Request Body DTO

我尝试从方法响应正文中获取派生的 class 字段。请求体参数是 base class 的类型。请求带有派生的 class 字段,但我无法将其转换为派生的 class.

这是我的控制器方法和 DTO classes:

方法:

 @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);
    }

基础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;

}

派生 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;
}

请求JSON是:

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

}

结果,服务 returns 错误:

{
"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 ?

您想要做的是尝试将 Parent 类型转换为 child,这被称为 向下转换。这仅在您将 Parent 作为 child 的实例时才有效。在您的情况下,PromotionDto 应该是 OrderThresholdDiscountPromotionDto.

的一个实例

请参考下面的例子:

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;

}

使用Jackson inheritance功能。注释PromotionDtoclass如下:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "promotionType")
@JsonSubTypes({
    @Type(value = OrderThresholdDiscountPromotionDto.class, name = "ORDER_THRESHOLD_DISCOUNT"),
})
class PromotionDto {

并删除:

private PromotionTypeEnum promotionType;

属性。它将由 Jackson 自动处理。在控制器中,您将能够使用 instanceof.