title: Jackson: Multiple Serializers on the same entity when differents Rest EndPoint are called
body: <p>I'm trying to avoid using the DTO antipattern when different EndPoint are called, where each returns a distinct representation of the same entity. I'd like to take advantage of the serialization that Jackson performs when I return the entity in the Rest EndPoint. This means that serialization is only done once and not twice as it would be with a DTO (entity to DTO and DTO to Json):</p>
  
  <p><strong>EndPoints example:</strong></p>
  
  <pre><code>@GetMapping("/events")
  public ResponseEntity&lt;List&lt;Event&gt;&gt; getAllEvents(){
      try {
          List&lt;Event&gt; events = (List&lt;Event&gt;) eventsRepository.findAll();
          return new ResponseEntity&lt;List&lt;Event&gt;&gt;(
                  events, HttpStatus.OK);
      }catch(IllegalArgumentException e) {
          return new ResponseEntity&lt;List&lt;Event&gt;&gt;(HttpStatus.BAD_REQUEST);
      }
  }
  
  @GetMapping("/events/{code}")
  public ResponseEntity&lt;Event&gt; retrieveEvent(@PathVariable String code){
      Optional&lt;Event&gt; event = eventsRepository.findByCode(code);
      return event.isPresent() ? 
              new ResponseEntity&lt;Event&gt;(event.get(), HttpStatus.OK) :
              new ResponseEntity&lt;Event&gt;(HttpStatus.BAD_REQUEST);
  }
  </code></pre>
  
  <p><strong>Serializer (class that extends of StdSerializer):</strong></p>
  
  <pre><code>@Override
  public void serialize(Event value, JsonGenerator gen, 
          SerializerProvider provider) throws IOException {
  
      if(firstRepresentation) {
          //First Representation
          gen.writeStartObject();
          gen.writeNumberField("id", value.getId());
          gen.writeObjectField("creation", value.getCreation());
  
          gen.writeObjectFieldStart("event_tracks");
          for (EventTrack eventTrack : value.getEventsTracks()) {
  
              gen.writeNumberField("id", eventTrack.getId());
              gen.writeObjectField("startTime", eventTrack.getStartTime());
              gen.writeObjectField("endTime", eventTrack.getEndTime());
              gen.writeNumberField("priority", eventTrack.getPriority());
  
              gen.writeObjectFieldStart("user");
              gen.writeNumberField("id", eventTrack.getUser().getId());
              gen.writeEndObject();
  
              gen.writeObjectFieldStart("state");
              gen.writeNumberField("id", eventTrack.getState().getId());
              gen.writeStringField("name", eventTrack.getState().getName());
              gen.writeEndObject();
  
          }
  
          gen.writeEndObject();
          gen.writeEndObject();
      }else if(secondRepresentation) {
         //Second Representation
      }
  }
  </code></pre>
  
  <p><strong>Entity:</strong></p>
  
  <pre><code>@JsonSerialize(using = EventSerializer.class)
  @RequiredArgsConstructor
  @Getter
  @Setter
  public class Event implements Comparable&lt;Event&gt;{
  
      private Long id;
  
      @JsonIgnore
      private String code;
  
      private Timestamp creation;
  
      @NonNull
      private String description;
  
      @JsonUnwrapped
      @NonNull
      private EventSource eventSource;
  
      @NonNull
      private String title;
  
      @NonNull
      private Category category;
  
      @NonNull
      @JsonProperty("event_tracks")
      private List&lt;EventTrack&gt; eventsTracks;
  
      @JsonProperty("protocol_tracks")
      private List&lt;ProtocolTrack&gt; protocolTracks;
  
      public void addEventTrack(@NonNull EventTrack eventTracks) {
          eventsTracks.add(eventTracks);
      }
  
      @JsonIgnore
      public EventTrack getLastEventTrack() {
          return eventsTracks.get(eventsTracks.size() - 1);
      }
  
      @JsonIgnore
      public int getLastPriority() {
          return getLastEventTrack().getPriority();
      }
  
      public void generateUUIDCode() {
          this.code = UUID.randomUUID().toString();
      }
  
      @Override
      public int compareTo(Event o) {
          return this.getLastPriority() - o.getLastPriority();
      }
  }
  </code></pre>
  
  <p>So, so far I have been able to serialize a representation type with a class that extend of StdDeserializer, but this doesn't give me the flexibility to extend the representations of the same entity attributes in multiple ways. Although I've tried it with Json annotations, but I realize that the more representations the entity class has, it can get very complex, something that it should be simple. Maybe some idea how I could do it.</p>
  
  <p>Thank you.</p>