How to deserialize an abstract class in a spring endpoint using the JsonSubTypes annotation?

by ola.stoltenberg , in category: Java , 2 years ago

How to deserialize an abstract class in a spring endpoint using the JsonSubTypes annotation in Java?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by sister , 2 years ago

@ola.stoltenberg Identify the classes which extends the abstract class :

1
2
3
4
5
6
7
8
@JsonSubTypes({
   @Type(value = InteractionDeclarationPresentation.class),
   @Type(value = InteractionDemandePresentation.class)
})
public abstract class InteractionPresentation {
   private List<Commentaire> comments;
   private Gestionnaire gestionnaireAffected;
}

Do not forget to call the constructor of the abstract class in the subclasses

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class InteractionDemandePresentation extends InteractionPresentation {

   public InteractionDemandePresentation(List<Commentaire> comments,
                                         Gestionnaire gestionnaireAffected,
                                         LocalDateTime creationDateTime,
                                         SinistrePresentation sinistre
   ) {
       super(comments, gestionnaireAffected);
   }
}


by alex.cummings , 7 months ago

@ola.stoltenberg 

To deserialize an abstract class using the JsonSubTypes annotation in a Spring endpoint, you need to follow these steps:

  1. Create the abstract class and define the common properties and methods that all subclasses should have. In your case, the InteractionPresentation class.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@JsonSubTypes({
   @Type(value = InteractionDeclarationPresentation.class),
   @Type(value = InteractionDemandePresentation.class)
})
public abstract class InteractionPresentation {
   // Define common properties and methods
   private List<Commentaire> comments;
   private Gestionnaire gestionnaireAffected;

   // Define constructors, getters, setters, and other necessary methods
}


  1. Create the concrete subclasses that extend the abstract class and specify the additional properties and constructors. Make sure to call the constructor of the abstract class in the subclass constructor using the super keyword. In your case, the InteractionDemandePresentation class.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class InteractionDemandePresentation extends InteractionPresentation {

   public InteractionDemandePresentation(List<Commentaire> comments,
                                         Gestionnaire gestionnaireAffected,
                                         LocalDateTime creationDateTime,
                                         SinistrePresentation sinistre
   ) {
       super(comments, gestionnaireAffected);
       // Set additional properties specific to this subclass
   }

   // Define additional getters, setters, and other necessary methods
}


  1. In your Spring endpoint, specify the abstract class as the parameter type or return type, and Spring will handle the deserialization.
1
2
3
4
@PostMapping("/interaction")
public void processInteraction(@RequestBody InteractionPresentation interaction) {
   // Handle the deserialized interaction object
}


  1. When sending the JSON data in the request body, make sure to include a property that determines the concrete subclass type. This property will be used by the JsonSubTypes annotation to correctly deserialize the object.


For example, if you are sending a JSON request with a InteractionDemandePresentation object, include a property like:

1
2
3
4
5
6
{
   "comments": [],
   "gestionnaireAffected": {},
   "creationDateTime": "2022-09-01T10:00:00",
   "sinistre": {}
}


Make sure to include all the necessary properties according to your class definition.


By following these steps and using the JsonSubTypes annotation, Spring will be able to correctly deserialize the abstract class based on the concrete subclass specified in the JSON data.