从基类请求主体DTO派生DTO

时间:2019-03-14 11:28:28

标签: java spring jackson polymorphism dto

我尝试从方法响应主体获取派生类字段。请求正文参数是基类的类型。请求带有派生类字段,但是我不能将其强制转换为派生类。

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

方法:

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

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;

}

派生类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"

}

结果是,服务返回错误:

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

}

  

我的问题是:有什么方法,库,注释等来获取   从请求派生的类实例?

2 个答案:

答案 0 :(得分:1)

使用Jackson inheritance功能。注释PromotionDto类,如下所示:

@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

答案 1 :(得分:-1)

您要执行的操作是尝试将父类型输入到一个称为 Downcasting 的孩子中。仅当您将父项作为子项的实例时,此选项才有效。就您而言, 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;

}