• notice
  • Congratulations on the launch of the Sought Tech site

Document how polymorphism is serialized and deserialized

Foreword: Recently, I encountered the problem of polymorphic serialization exception in the company project. The details are as follows:

1
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type , class com.xxxPayment]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.xxxPayment` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

In the project, users need to add their own payment methods, and the payment methods are divided into three types: ABC, which
inherits the Payment class. The interface code is as follows:

//Hide the request url and type
    @ApiOperation("Add payment method")
    public ResultInfo<? extends Payment> createPayment(
            @RequestBody Payment req) {

        return ResultInfo.success(xx.createPayment(req));
    }

The workaround is as follows:

//Add this information to the Payment class
@JsonTypeInfo (
         use = JsonTypeInfo.Id.NAME,
         include = JsonTypeInfo.As.EXISTING_PROPERTY,
         property = "payment"
  )
@JsonSubTypes (value = { @JsonSubTypes .Type(value = A . class , name = "A" ), @JsonSubTypes .Type(value = B . class , name = "B" ), @JsonSubTypes .Type(value = C .class , name = "C" ) }) public class
         
         
         

   Payment {
  //I won't write the specific fields

}

AVAILABLE ONLINE

  • @JsonTypeInfo– indicates details of what type information to include in serialization

  • @JsonSubTypes– indicates sub-types of the annotated type

  • @JsonTypeName– defines a logical type name to use for annotated class

IN THE ABOVE INFORMATION, THE MOST CRITICAL ANNOTATION TO SOLVE POLYMORPHISM IS @JSONTYPEINFO

  • use: What distinguishes the different types during serialization, and is used to convert to the type of the response during deserialization

    • JsonTypeInfo.Id.CLASS: use fully qualified class name for identification

    • JsonTypeInfo.Id.MINIMAL_CLASS: If the parent class and the child class are in the same package class, use the class name (ignore the package name) as the identifier

    • JsonTypeInfo.Id.NAME: a logical assigned name

    • JsonTypeInfo.Id.NONE: Do not use an identification code

  • include (optional): A mechanism for including type metadata, specifying how identifiers are included. It has the following optional values:

    • JsonTypeInfo.As.PROPERTY: as a sibling attribute of the data

    • JsonTypeInfo.As.EXISTING_PROPERTY: as a property that already exists in the POJO

    • JsonTypeInfo.As.EXTERNAL_PROPERTY: as an extended attribute

    • JsonTypeInfo.As.WRAPPER_OBJECT: as a wrapped object

    • JsonTypeInfo.As.WRAPPER_ARRAY: as a wrapped array

  • property (optional): a custom type-distinguishing id, the default is @Class, this property is only available when:

    • useis JsonTypeInfo.Id.CLASS(if property is not specified, it defaults to @class), JsonTypeInfo.Id.MINIMAL_CLASS(if property is not specified , it defaults to @class), (if property is not specified, it defaults to @type) JsonTypeInfo.Id.NAME,

    • includeIt is valid only when JsonTypeInfo.As.PROPERTYJsonTypeInfo.As.EXISTING_PROPERTY,
      JsonTypeInfo.As.EXTERNAL_PROPERTY

  • defaultImpl (optional): If the type identifier does not exist or is invalid, you can use this attribute to specify the default type used when deserializing

  • visible (optional): Whether the visible (default false) property defines whether the value of the type identifier will be part of the deserializer through the JSON stream, the default is false, that is, jackson will process and delete from the JSON content The type identifier is then passed to the JsonDeserializer


@JSONSUBTYPES

Acts on classes/interfaces to list subclasses of a given class. It is only used when the subclass type cannot be detected. It is generally used with @JsonTypeInfo on the base class.
@JsonSubTypesThe value of is an @JsonSubTypes.Type[]array, which enumerates the polymorphic type (value corresponds to the subclass) and the identifier value of the type (the value @JsonTypeInfoof the property identifier name corresponding to the name, this is an optional value, if not specified, it needs to be specified @JsonTypeNamein the subclass formulated above)



Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy atorvastatin online cheap & lt;a href="https://lipiws.top/"& gt;atorvastatin 80mg over the counter& lt;/a& gt; buy atorvastatin 80mg without preion

Ogwwoq

2024-03-10

Leave a Reply

+