一种使用 Postman 的多态 HTTP 请求方式?

A way of polymorphic HTTP Requests using Postman?

我正在尝试使用 Java 的多态性。我用 Spring Boot 构建了一个简单的 CRUD 应用程序,我想用 Postman 测试它...

问题是,我遇到了下一个异常:

WARN 4576 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve subtype of [simple type, class com.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.balabasciuc.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 25] (through reference chain: com.shoppingprojectwithhibernate.PromotionsModule.Domain.Promotion["promotionSeason"])]

当我尝试向端点发出 POST 请求时,该端点 return 是一个接口,在运行时可以是任何子类型,我在这里滚动寻找解决方案,我使用了 jackson从其他解决方案中使用,但 none 到目前为止对我有用,你能给我一个提示吗?

my interface:

    import com.fasterxml.jackson.annotation.JsonSubTypes;
    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "promotionSeason")
    @JsonSubTypes(
            {
                    @JsonSubTypes.Type(value = PromotionChristmasSeason.class, name = "christmasPromotion"),
                  @JsonSubTypes.Type(value = PromotionEasterSeason.class, name = "easterPromotion"),
                    @JsonSubTypes.Type(value = NoPromotionForYouThisTimeMUHAHA.class, name = "noPromotion")
            })
    public interface PromotionSeason {
    
        String isSeason();

Interface subtype:
    @JsonTypeName(value = "noPromotion")
    public class NoPromotionForYouThisTimeMUHAHA implements PromotionSeason {
    



Promotion class Entity constructor:
    public Promotion(int numberOfProductsAtPromotion, PromotionSeason promotionSeason) {
        this.numberOfProductsAtPromotion = numberOfProductsAtPromotion;
        this.promotionSeason = promotionSeason;
    }

Rest Controller class

@RestController
@RequestMapping(value = "/promotions")
public class PromotionController {

    @PostMapping(value = "/createPromotion")
    public ResponseEntity<String> createPromotion(@RequestBody Promotion promotion)
    {
        promotionService.createPromotion(promotion);
        return ResponseEntity.status(HttpStatus.CREATED)
                .body("Done");
    }
    
    Postman request:
    {
        "numberOfProductsAtPromotion" : 20,
        "promotionSeason" : "noPromotion"
    }
    
    Postman response:
    {
        "timestamp": "2022-05-04T18:58:06.873+00:00",
        "status": 400,
        "error": "Bad Request",
        "path": "/promotions/createPromotion"
    }
    
    Spring Response:
    2022-05-04 21:19:44.374  WARN 4576 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve subtype of [simple type, class com.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.balabasciuc.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 25] (through reference chain: com.shoppingprojectwithhibernate.PromotionsModule.Domain.Promotion["promotionSeason"])]

NoPromotionFor YouThisTimeMUHAHA 有构造器吗?如果在所有实现中都有 'Type',则可以将接口更改为抽象 class 并在此处添加变量。

一直以来都是我的错误 Post 请求,而不是:

Post人工请求:

{
    "numberOfProductsAtPromotion" : 20,
    "promotionSeason" : "noPromotion"    }

应该是:

{
    "numberOfProductsAtPromotion" : 20,
    "promotionSeason" : { // "promotionSeason" is class instance var from Promotion Entity class
        "promotionSeason" : "noPromotion" // "promotionSeason" is defined on interface level: `@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "promotionSeason")`
         //"noPromotion" is defined on interface and subtype levels
        //any more subtype(subclass) variables to init if any
    }

检查这个:https://www.youtube.com/watch?v=IlLC3Yetil0