Spring 使用 Jackson 注释启动多态请求体

Spring boot polymorphic request body using Jackson annotations

我有一个 OpenAPI v3 规范并为我的一个端点定义了一个 'anyof' 主体。

我正在使用从 swagger-codegen v3 生成的模型 classes,他们使用 Jackson 注释来定义这些可能的请求主体的 JsonSubTypes。

在我的控制器 class 中,我使用 instanceof 来检查我正在使用哪个主体,但是我无法将 Parent class 主体转换为 child class pre-compile 时间,但我想将请求主体定义为 child class pre-compile 时间,以将其作为参数传递给我的服务。

在这种情况下,创建转换器 classes 来转换 Parent -> Child 是否更好?

示例:

政策Parentclass

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@NoArgsConstructor
@Getter
@Setter
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = 
JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = AutoInsurance.class, name = "AUTO_INSURANCE"),
    @Type(value = LifeInsurance.class) })
public class Policy {

    @Id
    @GeneratedValue
    private Long id;
    private String policyNumber;
    @Enumerated(EnumType.STRING)
    private PolicyType policyType;
    private String name;

}

汽车保险class:

@Entity
@NoArgsConstructor
@Getter
@Setter
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonTypeName(value = "AUTO_INSURANCE")
public class AutoInsurance extends Policy {

    @Id
    @GeneratedValue
    private Long id;
    private String vehicleNumber;
    private String model;
    private String vehicleType;
    private String vehicleName;

}

人寿保险class:

@Entity
@NoArgsConstructor
@Getter
@Setter
@JsonTypeName(value = "LIFE_INSURANCE")
public class LifeInsurance extends Policy {

    @OneToMany(mappedBy = "policy")
    private List<Dependents> dependents;
    private String medicalIssues;
    private String medication;
    private String treatments;

}

控制器:

@RestController
@RequestMapping("/policy")
public class PolicyController {

    @PostMapping
    public void savePolicy(@RequestBody Policy policy) {
        if (policy instanceof AutoInsurance) {
             AutoInsurance autoInsurance = (AutoInsurance) policy;
             service.processAutoInsurance(autoInsurance)
             ...
        } else if (policy instanceof LifeInsurance) {
             LifeInsurance lifeInsurance = (LifeInsurance) policy;
             service.processlifeInsurance(lifeInsurance)
             ...
        }
    }
}

我找到了解决这个问题的方法。

我们可以使用 Jackson 的 ObjectMapper 转换方法将父 类 转换为子 类。

ObjectMapper 文档:https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html

控制器示例:

@RestController
@RequestMapping("/policy")
public class PolicyController {
    @Autowired
    private ObjectMapper objectMapper; // Define a ObjectMapper bean in your configuration class

    @PostMapping
    public void savePolicy(@RequestBody Policy policy) {
        if (policy instanceof AutoInsurance) {
             AutoInsurance autoInsurance = objectMapper.convertValue(policy, AutoInsurance.class);
             service.processAutoInsurance(autoInsurance);
             ...
        } else if (policy instanceof LifeInsurance) {
             LifeInsurance lifeInsurance = objectMapper.convertValue(policy, LifeInsurance.class);
             service.processlifeInsurance(lifeInsurance);
             ...
        }
    }
}