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 identificationJsonTypeInfo.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 identifierJsonTypeInfo.Id.NAME
: a logical assigned nameJsonTypeInfo.Id.NONE
: Do not use an identification codeinclude (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 dataJsonTypeInfo.As.EXISTING_PROPERTY
: as a property that already exists in the POJOJsonTypeInfo.As.EXTERNAL_PROPERTY
: as an extended attributeJsonTypeInfo.As.WRAPPER_OBJECT
: as a wrapped objectJsonTypeInfo.As.WRAPPER_ARRAY
: as a wrapped arrayproperty (optional): a custom type-distinguishing id, the default is @Class, this property is only available when:
use
isJsonTypeInfo.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
,include
It is valid only whenJsonTypeInfo.As.PROPERTY
,JsonTypeInfo.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.@JsonSubTypes
The 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 @JsonTypeInfo
of the property identifier name corresponding to the name, this is an optional value, if not specified, it needs to be specified @JsonTypeName
in the subclass formulated above)
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